Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have this string:

a,bcde,fgh,ijk,lmno,pqrst,uv

I need a JavaScript function that will split the string by every , but only those that don't have a before them

How can this be done?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
175 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here's the shortest thing I could come up with:

'a\,bcde,fgh,ijk\,lmno,pqrst\,uv'.replace(/([^\]),/g, '$1u000B').split('u000B')

The idea behind is to find every place where comma isn't prefixed with a backslash, replace those with string that is uncommon to come up in your strings and then split by that uncommon string.

Note that backslashes before commas have to be escaped using another backslash. Otherwise, javascript treats form , as escaped comma and produce simply a comma out of it! In other words if you won't escape the backslash, javascript sees this: a,bcde,fgh,ijk,lmno,pqrst,uv as this a,bcde,fgh,ijk,lmno,pqrst,uv.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...