Edge7
Edge7

Reputation: 681

Extract string between two strings

I have the following problem:

var previous_state = response.match(simulator.previousStateString+"(.*?),");             
if( ! previous_state ) {response.match(simulator.previousStateString+"(.*?) ")};
if( ! previous_state ) {response.match(simulator.previousStateString+"(.*?)#")};

Basically, I am looking for a string between simulator.previousStateString and ',' or ' ' or'#'. Is there the possibility to have a more compact code? I mean, can I put all 3 regex in just one?

Thanks

Upvotes: 1

Views: 132

Answers (1)

anubhava
anubhava

Reputation: 784998

Sure you can use this regex with character class:

response.match( new RegExp(simulator.previousStateString + "(.*?)[, #]") ); 

It matches only one out of several characters inside [ and ]

PS: Make sure there is no special regex character in simulator.previousStateString

Upvotes: 7

Related Questions