I want to remove the single quote below after 10 and replace with empty string.
So 100,50,"bla 10" bla",20,"another string"
becomes
100,50,"bla 10 bla",20,"another string"
I was so close but the following is selecting 0"
/[^"]"[^$"]/g
Any thoughts? Thank you!
You may use
var s = '100,,50,"bla 10" bla",20,"another string","string"'; var splits = s.split(/(?:^|,)(".*?")(?:,|$)|,/); splits = splits.filter(function (x) { return x !== undefined;}); console.log(splits); var res = splits.map(function(x) { return x.replace(/^,*(")|("),*$|"/g, '$1$2'); }).join(','); console.log(res);
Details
(?:^|,)"
- match ,
or start of string (
- start of the capturing group "
- a "
.*?
- any 0+ chars, as few as possible "
- a "
)
- end of the capturing group (?:,|$)
- a ,
or end of string |
- or ,
- a comma After we get all the matches, the .replace(/^,*(")|("),*$|"/g, '$1$2'
removes all "
but at the start/end of the matches. Its details:
^,*(")|
- matches ,
1 or more times at the start of the string ( ^
) and then captures a "
into Group 1, or ("),*$|
- captures a "
at the end of the string ( $
) and then 0+ ,
s into Group 2 or "
- matches any other "
. The $1$2
replacement pattern "restores" the "
s captured into Group 1 and 2 (ie keeps the "
s that are either at the start of the string with an optional ,
in front or end of the string and removes all others).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.