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 badges
Add a ment  | 

3 Answers 3

Reset to default 5

Why 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