admin管理员组文章数量:1328582
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!
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!
Share Improve this question edited Sep 1, 2011 at 8:20 Reporter 3,9485 gold badges35 silver badges49 bronze badges asked Sep 1, 2011 at 8:16 JohnJohn 111 silver badge3 bronze badges4 Answers
Reset to default 4if (/DPObject\d+/.test(string)) {....}
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 .
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"
There are a couple of ways:
- Use Javascripts indexOf method
- Use Javascript Regular Expressions
- 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.
本文标签: javascriptJScript how to check if a string contains a specific wordStack Overflow
版权声明:本文标题:javascript - JScript: how to check if a string contains a specific word? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742258766a2442120.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论