Skip to content
Advertisement

JavaScript regex get number after string

After coming to the shocking realization that regular expressions in JavaScript are somewhat different from the ones in PCE, I am stuck with the following.

In php I extract a number after x:

(?x)[0-9]+

In JavaScript the same regex doesn’t work, due to invalid group resulting from the capturing parenthesis difference.

So I am trying to achieve the same trivial functionality, but I keep getting both the x and the number:

(?:x)([0-9]+)

How do I capture the number after x without including x?

Advertisement

Answer

This works too:

/(?:x)([0-9]+)/.test('YOUR_STRING');

Then, the value you want is:

RegExp.$1 // group 1

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement