1

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

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

To quote special regex characters see this Q&A: stackoverflow.com/questions/2593637/…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.