Split words with JavaScript, but skips some words before "\"
I have a string:
"a","b","c"开发者_运维知识库
and I can split those words to:
a b c
using Javascript, but how about:
"a,","b\"","c\,\""
How do I get:
a, b" c,"
Still not sure if this is one string? But if it is... and this is the only instance of it that you want manipulated, you could use this super convoluted way....
var a = '"a,","b\"","c\,\""';
var b = a.replace(/"/,'');
//get rid of the first "
var c = b.lastIndexOf('"');
//find the last "
var d = b.substring(0,c);
//cut the string to remove the last "
var e = d.split('","');
//split the string at the ","
for(var i="0"; i<e.length; i++){
document.write(e[i] + '<br />');
}
Example: http://jsfiddle.net/jasongennaro/ceVG7/1/
- Split as you were
- Replace any
\
in each resultant string, after the split (usingreplace()
)
You cannot define "a","b","c" as one string. If you have a string then,
The replace() method searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring.
string.replace(regexp/substr,newstring)
The split() method is used to split a string into an array of substrings, and returns the new array.
string.split(separator, limit)
or var myString = "abc,efg";
var mySplitResult = myString.split(",");
mySplitResult[0] will be abc
mySplitResult[0] will be efg
What about this:
'"a,","b\"","c\,\""'.replace(/^"|"$/g,'').split(/"\s*,\s*"/).join('<br/>')
that gives:
a,<br/>b"<br/>c,"
<br/>
can be replaced by \n
if the output is not HTML
精彩评论