1

I am trying to find a way to check if a string contains a specific sequence of characters in JScript. In my case, I am trying to see if the string is "DPObject" followed by a number. Such as "DPObject3" or "DPObject14".

Thank you!

4 Answers 4

4
if (/DPObject\d+/.test(string)) {....}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that helped me just an hour ago :)
1

Then you should use a regular expression. I think this would be something like :

var re = new RegExp("^DPObject([0-9]+)$");
re.test(someString);

This ensures there is at least only one digit after DPObject. The "^" at the beginning is to ensure the string starts with DPObject. Check references on regexps for this kind of problems :) edit: added "$" to mark the end of the string, the updated should be more "solid"

Comments

0

Javascript String has an indexOf method you can use to check if a String contains a particular substring .

If you need to test for patterns , like "DPObject" followed by an integer , probably you need to use Regexes . ( http://www.regular-expressions.info )

It's javascript , or js for short - not JScript .

1 Comment

This question is about JScript. Not javascript. So your response is unhelpful and incorrect.
0

There are a couple of ways:

  1. Use Javascripts indexOf method
  2. Use Javascript Regular Expressions
  3. Use JQuery's contains function

Regular expressions are the most powerful and elegant way of doing it. They syntax makes sense after a while (honestly). ;-)

Good luck.

Comments

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.