0

I have this string

'bookmarkState={"params":{"date_from":"2014-07-31","date_to":"2014-10-01"}}'

I want to replace 2014-07-31 with 2014-01-01, i.e. the substring contained between '"date_from":"' and '","', using a regular expression in javascript. I have written this code but it doesn't work:

var qs = 'bookmarkState={"params":{"date_from":"2014-07-31","date_to":"2014-10-01"}};'
var regEx = /^(.*?date_from":")[^"]*(".*)$/;
qs = qs.replace(regEx, '2014-01-01');`

2 Answers 2

2

You don't need a regex to do that:

eval('bookmarkState={"params":{"date_from":"2014-07-31","date_to":"2014-10-01"}}');

bookmarkState.params.date_from = '1988-04-12';

console.log(JSON.stringify(bookmarkState));
Sign up to request clarification or add additional context in comments.

Comments

0
^(.*?date_from":")[^"]*(",".*)$

Try this.Replace by $1<your string>$2.See demo.

http://regex101.com/r/qZ0uP0/1

5 Comments

I write this code but it doesn't work var qs = bookmarkState={"params":{"date_from":"2014-07-31","date_to":"2014-10-01"}}'; var regExp = /^(.*?date_from":")[^"]*(".*)$/; qs = qs.replace(regExp, '2014-01-01');
@RobertoGentili you need to use replace()
@RobertoGentili you need to use flag re.Multiline
var qs = 'bookmarkState={"params":{"date_from":"2014-07-31","date_to":"2014-10-01"}}'; var regEx = new RegExp('^(.*?date_from":")[^"]*(".*)$', 'm'); qs = qs.replace(regExp, '2014-01-01'); Doesn't works (flag m setted)
@RobertoGentili replace text will be $1<something>$2 and what is the error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.