Splitting A String In Js Where A Pattern Does Not Match?
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
Solution 1:
Match the pattern. don't split with it.
value=value.match(/http\:\/\/.+/g)
(.+matches everything to the end of a line)
Solution 2:
Solved finally! Here is the code:
functionsplit_lines() {
var oText = $('linkTxtArea').value;
removeBlankLines(); // a helper function to remove blank lines
oText = oText.split("\n"); // first split the string into an arrayfor (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;
}
Post a Comment for "Splitting A String In Js Where A Pattern Does Not Match?"