5

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?

3
  • 2
    This should work (?!x)([0-9]+). Commented Feb 24, 2016 at 16:26
  • @gothical yes it does! Commented Feb 24, 2016 at 16:27
  • (?!x)([0-9]+) = ([0-9]+). And (?x)[0-9]+ is also equal to [0-9]+. To get a sequence of digits after an x char in PHP/JS, you can just use /x(\d+)/ and grab Group 1 value. Or, use a lookbehind, /(?<=x)\d+/ Commented Jul 26, 2021 at 7:20

5 Answers 5

4

This works too:

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

Then, the value you want is:

RegExp.$1 // group 1

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

4 Comments

yes that sort of did what I was hoping for: var x = str.match(/(?:x)(\d+)/ to match the correct part, and then accessing it with x[1]. I'm sure there must be a more elegant way?
A more elegant way? Dude, I find this beautiful! :D
Seriously now, the way I wrote it is a very common way of using regex in Javascript.
I was hoping that I wouldn't have to access an array but a single variable. This approach (which I thank you for) requires that I test the array length. Surely there is a way to do this with the regex? ATM it returns both x15 and then the second capture group 15.
4

You can try the following regex: (?!x)[0-9]+

fiddle here: https://jsfiddle.net/xy6x938e/1/

This is assuming that you are now looking for an x followed by a number, it uses a capture group to capture just the numbers section.

var myString = "x12345";
var myRegexp = /x([0-9]+)/g;
var match = myRegexp.exec(myString);
var myString2 = "z12345";
var match2 = myRegexp.exec(myString2);

if(match != null && match.length > 1){
    alert('match1:' + match[1]);
}
else{
    alert('no match 1');
}
if(match2 != null && match2.length > 1){
    alert('match2:' + match2[1]);
}
else{
    alert('no match 2');
}

1 Comment

actually this is wrong, it will match any number, ignoring whether its x, y or z
3

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

In fact, you just want to extract a sequence of digits after a fixed string/known pattern.

Your PCRE (PHP) regex, (?x)[0-9]+, is wrong becaue (?x) is an inline version of a PCRE_EXTENDED VERBOSE/COMMENTS flag (see "Pattern Modifiers"). It does not do anything meaningful in this case, (?x)[0-9]+ is equal to [0-9]+ or \d+.

You can use

console.log("x15 x25".match(/(?<=x)\d+/g));

You can also use a capturing group and then extract Group 1 value after a match is obtained:

const match = /x(\d+)/.exec("x15");
if (match) {
  console.log(match[1]); // Getting the first match
}

// All matches
const matches = Array.from("x15,x25".matchAll(/x(\d+)/g), x=>x[1]);
console.log(matches);

Comments

2

(\d+) try this! i have tested on this tool with x12345 http://www.regular-expressions.info/javascriptexample.html

Comments

0

You still can use exclusive pattern (?!...)

So, for your example it will be /(?!x)[0-9]+/. Give a try to the following:

/(?!x)\d+/.exec('x123')
// => ["123"]

1 Comment

The problem with this regex (as commented at jimplode's post) is that it matches any number following a character, regardless if the key is x, y or z. See for yourself: regex101.com/r/oP2eR3/1

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.