9

I'm a bit rusty on my regex and javascript. I have the following string var:

var subject = "javascript:loadNewsItemWithIndex(5, null);";

I want to extract 5 using a regex. This is my regex:

/(?:loadNewsItemWithIndex\()[0-9]+/)

Applied like so:

subject.match(/(?:loadNewsItemWithIndex\()[0-9]+/)

The result is:

loadNewsItemWithIndex(5

What is cleanest, most readable way to extract 5 as a one-liner? Is it possible to do this by excluding loadNewsItemWithIndex( from the match rather than matching 5 as a sub group?

3 Answers 3

8

The return value from String.match is an array of matches, so you can put parentheses around the number part and just retrieve that particular match index (where the first match is the entire matched result, and subsequent entries are for each capture group):

var subject = "javascript:loadNewsItemWithIndex(5, null);";
var result = subject.match(/loadNewsItemWithIndex\(([0-9]+)/);
                                    //             ^      ^  added parens
document.writeln(result[1]);
                  //    ^ retrieve second match (1 in 0-based indexing)

Sample code: http://jsfiddle.net/LT62w/

Edit: Thanks @Alan for the correction on how non-capturing matches work.

Actually, it's working perfectly. Text that's matched inside a non-capturing group is still consumed, the same as text that's matched outside of any group. A capturing group is like a non-capturing group with extra functionality: in addition to grouping, it allows you to extract whatever it matches independently of the overall match.

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

3 Comments

The non-capturing match apparently doesn't work properly. I knew there were some odd regex behaviours in javascript, but I though this problem was me being a bit rusty.
Actually, it's working perfectly. Text that's matched inside a non-capturing group is still consumed, the same as text that's matched outside of any group. A capturing group is like a non-capturing group with extra functionality: in addition to grouping, it allows you to extract whatever it matches independently of the overall match.
...And the indexing on the result array matches regex "group" numbers (which start at 1).
4

I believe the following regex should work for you:

loadNewsItemWithIndex\(([0-9]+).*$

var test = new RegExp(/loadNewsItemWithIndex\(([0-9]+).*$/);
test.exec('var subject = "javascript:loadNewsItemWithIndex(5, null);";');

The break down of this is

loadNewsItemWithIndex = exactly that

\( = open parentheses 

([0-9]+) = Capture the number
.* = Anything after that number

$ = end of the line

2 Comments

You were correct. I added it when I was thinking it out in my head and never removed it.
so is .*$ for the same reason
3

This should suffice:

<script>
var subject = "javascript:loadNewsItemWithIndex(5, null);";
number = subject.match(/[0-9]+/);
alert(number);
</script>

1 Comment

Best answer with the context provided

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.