I have the following string in javascript,
Id:121,RefId:123,Date:Sep 22, 2012 12:00:00 AM
when i tried to split it into 3 key,value pairs using , as the separator it is giving me wrong out put because in date there is another ,(comma).
So i guess i must use regular expression.
I want to display it as follows;
Id:121
RefId:123
Date:Sep 22, 2012 12:00:00 AM
Can anyone please suggest how to overcome the extra comma in date using regular expression?
Thanks in Advance...
You mean split on the ,
which is followed not the white space?
'Id:121,RefId:123,Date:Sep 22, 2012 12:00:00 AM '.split(/,(?=\S)/);
// will give you ["Id:121", "RefId:123", "Date:Sep 22, 2012 12:00:00 AM "]
if you really want a regular expression (instead of a limited split) you could do this:
var text = "Id:121,RefId:123,Date:Sep 22, 2012 12:00:00 AM";
text.match(/^(.+?),(.+?),(.+)$/);
If you would like to use a regular expression, you can try this:
re = /^Id:(\d+),RefId:(\d+),Date:(.+)/
matches = re.exec("Id:121,RefId:123,Date:Sep 22, 2012 12:00:00 AM")
matches[1] // "121"
matches[2] // "123"
matches[3] // "Sep 22, 2012 12:00:00 AM"
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.