i use this javascript code to editing paragraph if it have links the code will make the link active to click by adding html href element
my problem is the result of this code be in one line
i want the paragraph result stay paragraph not 1 line
any idea?
thanks all
var collection = document.getElementsByClassName("Edit_desc");
for (let i=0; i<collection.length; i++) {
//Regex Expresson to match general URL used frequently.
var expression = /[-a-zA-Z0-9#:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()#:%_\+.~#?&//=]*)?/gi;
//Creating Regex Object with the Regex Expression
var regex = new RegExp(expression);
//Fetch words from the Text Area , array named 'input_text_words' will contain all words separately
var input_text_words = collection[i].textContent.trim().split(/\s+/);
var input_text_number_of_words = input_text_words.length;
//Empty Paragraph Element
var paragraphElement = "";
//Traversing the words array
for(var index = 0 ; index < input_text_number_of_words ; index++){
//If the word matches the URL regex than print using so that link may appear.
if(input_text_words[index].match(regex)){
paragraphElement += ''+input_text_words[index]+'';
}
//Else if the word is not any url , simply print.
else{
paragraphElement += input_text_words[index];
}
if(index != input_text_number_of_words-1){
paragraphElement += ' ';
}
}
//Finally show the modified text in tha empty paragraph element.
collection[i].innerHTML = paragraphElement;
}
I suppose that your spaces are not caused by html tags like <br/>
So the problem is the splitting you do :
var input_text_words = collection[i].textContent.trim().split(/\s+/);
The ("string1 string2\nstring3").split(/\s+/); expression splittes the string into 3 items. Making you lose the lines part...
As a solution i suggest you use another regex that partially solves the problem
.split(/[^\S\r\n]/g)
witch i got from this guy
But this solution has possible issues, if the linke is at the start of the paragraph or if you can have a link with lines inside it ( done by a text-area for example ) that wont be selected. So be sure to check what do u need exactly and adjust the regex to something that suites you.
Related
im trying to remove all text except one in javascript
the idea is do something like
//jQuery("someelement").attr("class");
var classes= "cnode_1 timestamp_1413504000 item";
classes = classes.replace(/^(?!timestamp_)/,'');
i want to take only the text who starts with timestamp_, the expected ouput is :
timestamp_1413504000
i want this , then to grab the number
//now "classes" show be timestamp_1413504000
classes = classes.replace("timestamp_","");
the expected ouput is :
1413504000
i want to avoid use something like, split the clasess base on space, then use for bucle, and finally compare with indexOf
Do you need the "timestamp_" for something?
Why not just
= classes.replace(/^.*timestamp_(\d+).*/img, "$1");
Just use .match to get the part you want.
var classes= "cnode_1 timestamp_1413504000 item";
// match will return the matched string and the groups in an array
var ts = classes.match(/timestamp_(\d+)/)[1]; // match[1] is the group (\d+)
// 1413504000
I know you said that you want to avoid splitting or using indexOf, but javaScript doesn't have a 'startsWith' function. Why do you want to avoid doing that? Is the following unacceptable?
var NumberOutput;
var classList = document.getElementById('elementYouAreLookgingAt').className.split(/\s+/);
for (var i = 0; i < classList.length; i++) {
if (classList[i].indexOf("someClass") == 0) {
NumberOutput = classList[i].replace("someClass", "");
}
}
I want to have a text that overflows in another div, so I found and used this answer from another question at Stackoverflow.
The problem is that only plain text is displayed; links, bold/italics and paragraphs are ignored.
Here is the same jsfiddle from the answer, but with added html tags. How do i get to display them?
Code:
var currentCol = $('.col:first');
var text = currentCol.text();
currentCol.text('');
var wordArray=text.split(' ');
$.fn.hasOverflow = function() {
var div= document.getElementById( $(this).attr('id') );
return div.scrollHeight>div.clientHeight;
};
for ( var x = 0; x < wordArray.length; x++ ) {
var word = wordArray[x];
currentCol.append(word+' ');
if ( currentCol.hasOverflow() ) {
currentCol = currentCol.next('.col');
}
}
Any tips or advice will be appreciated :)
jQuery .text method returns only plain text. Try using .html instead.
Example:
var text = currentCol.html();
But if your tags contain any spaces (like <span class="some-class">) then the following line from your code will do a mess with your text
var wordArray=text.split(' ');
You might want to change it to
text = text.replace(/ (?![^<>]*>)/gi, '%^%');
var wordArray = text.split('%^%');
This is kind of workaround since you could iterate over each regex match and substring it on every space character but IMHO the above idea with %^% combination is much more simple. you can replace those 3 signs with anything you want. I just thought it is unique enough that won't be used in any other purpose.
Above regular expression is simplified and assumes that you don't use < and > signs in your text.
If you actually do I would recommend to replace them with < and > anyway.
I need to compare each line of textarea "a" with all lines of textarea "b" , and create an alert() with all items of "a" who doesn't exists in "b"
¿ How can i do this ?
One way could be to get the values of the 2 textareas and split them on newline (\n) characters. You can then iterate over the lines of the first textarea and check to see if they appear in the second textarea:
var t1 = document.getElementById("textarea1").value.split("\n"),
t2 = document.getElementById("textarea2").value.split("\n"),
notIn2 = [];
for(var i = 0; i < t1.length; i++) {
if(t2.indexOf(t1[i]) === -1) {
notIn2.push(t1[i]);
}
}
There may well be a better way, and I'm sure the above is far from perfect. This is just what came to mind first. Here's a working example.
here is my code:
var keys = keyword.split(' ');
//alert(keys);
for(var i=0; i<keys.length; i++)
{
var re = new RegExp(keys[i], "gi");
var NewString = oldvar.replace(re, '<span style="background-color:#FFFF00">'+keys[i]+'</span>');
document.getElementById("wordlist").innerHTML=NewString;
alert(keys[i]);
}
but here if I put a string "a b"; its split into two letters "a" and "b"
and this replace function replace "a" but when it get "b" it overwrite and only replace "b".
but I want to highlight both "a" and "b".
how to solve this?
I got another problem . If I replace/highlight it then it replace all "a" and "b" of HTML tag. so, how to prevent to replace those html tag. but also when I display the whole text I need all html tag
You can actually do a single regex replace like this:
var re = new RegExp(keys.join("|"), "gi");
oldvar = oldvar.replace(re, replacer);
document.getElementById("wordlist").innerHTML = oldvar;
function replacer(str)
{
return '<span style="background-color:#FFFF00">' + str + '</span>';
}
Example - http://jsfiddle.net/zEXrq/1/
What it is doing is merging all keys into a single regex seperated by | which will match all the words then running the replacer function on the matches.
Example 2 - http://jsfiddle.net/zEXrq/2/
var keys = keyword.split(' ');
//alert(keys);
for(var i=0; i<keys.length; i++)
{
var re = new RegExp(keys[i], "gi");
oldvar = oldvar.replace(re, '<span style="background-color:#FFFF00">'+keys[i]+'</span>');
document.getElementById("wordlist").innerHTML=oldvar;
alert(keys[i]);
}
Edit:
It seems obvious that oldvar is not changed durring the loop always only last replace is applyied. You have to change "oldvar" in order to replace all the words
You should do the Operations on the same var. You take oldvar outside of the loop, but never take the changed content into oldvar. So the last iteration (only) is the one which replaces the content.
You're calling replace on the variable oldvar (which is not declared in this snippet) in each iteration and thus starting from the same point - the non-highlighted string - every time. Without having seen all of the code, I would guess that simply replacing var NewString = with oldvar = and .innerHTML=NewString with .innerHTML=oldvar will solve your problem.
i am trying to split a TextArea value where a pattern does not match
the text is like following:
Some Good Tutorials
http://a.com/page1
http://a.com/page2
Some Good Images
http://i.com/p1
http://i.com/p2
Some Good Videos
http://m.com/p1
http://m.com/p2
now i want to get only the links from the text so a better solution would be to split the whole string in an array of strings where the a line is not a url and then from amongst this array split each string with "\n"
edit:
okay i found a solution, i can find lines which does not begin with http:// or https:// and replace them with a good place holder after than i can get the links
though i am weak in regex so can someone tell me how to do this in javascript?
Match the pattern. don't split with it.
value=value.match(/http\:\/\/.+/g)
(.+matches everything to the end of a line)
Solved finally! Here is the code:
function split_lines() {
var oText = $('linkTxtArea').value;
removeBlankLines(); // a helper function to remove blank lines
oText = oText.split("\n"); // first split the string into an array
for (i = 0; i < oText.length; i++) // loop over the array
{
if (!oText[i].match(/^http:/)) // check to see if the line does not begins with http:
{
oText[i] = oText[i].replace(oText[i], "!replaced!"); // replace it with '!replaced!'
}
}
oText = oText.toString().split("!replaced!"); // now join the array to a string and then split that string by '!replaced!'
for (i = 1; i < oText.length; i++)
{
oText[i] = oText[i].replace(/^,/,"").replace(/,$/,""); // there were some extra commas left so i fixed it
}
return oText;
}