admin管理员组文章数量:1399770
I've got a string. Let's say "This response happened in 90ms. Response finished".
I need to be able to parse out the "90ms" from the string.
The regex needs to match against an arbitrary number of numbers followed by an exact string match, in this case the string would be "ms".
Any help would be greatly appreciated.
I've got a string. Let's say "This response happened in 90ms. Response finished".
I need to be able to parse out the "90ms" from the string.
The regex needs to match against an arbitrary number of numbers followed by an exact string match, in this case the string would be "ms".
Any help would be greatly appreciated.
Share Improve this question asked Aug 8, 2014 at 15:57 OluOlu 3,5562 gold badges21 silver badges11 bronze badges3 Answers
Reset to default 5Why don't you try this regex?
\d+ms
\d+
- matches one or more digits.
ms
- Matches the string ms
Example:
> "This response happened in 90ms. Response finished".match(/\d+ms/)[0];
'90ms'
Use this regexp:
\d+ms
\d
matches a digit, +
means to match 1 or more of them, and ms
matches that exact string.
Now I suggest you go read a tutorial on regular expressions, there's one at regular-expressions.info. This is about as basic as it gets, so if you need to ask about things like this you'll be here every day, getting us to write your code one line at a time.
To extract the expression you need to use this:
var rx = /(\d+ms)/;
var arr = rx.exec(your_string);
extracted= arr[1];
The extracted string is what is between parenthesis. If you need only the number change de rx to /(\d+)ms/.
本文标签: javascriptRegex to find number followed by exact string matchStack Overflow
版权声明:本文标题:javascript - Regex to find number followed by exact string match - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744230804a2596322.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论