user2100029
user2100029

Reputation:

Replace a substring contained between two substring in javascript

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');`

Upvotes: 0

Views: 64

Answers (2)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

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));

Upvotes: 2

vks
vks

Reputation: 67968

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

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

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

Upvotes: 0

Related Questions