0

I extracted a string and i used regex to return a number, but it returns each number separated by a comma. I want to use regex, so I am confused, please help.

var getPages = "Pages (226)";
var getPagesNew = getPages.match(/\d/g);

This code returns 2,2,6

I need the number without commas, how do I do that? I tried using replace to remove the commas and replace them with nothing but that gave me some error.

3 Answers 3

1

Try: /\d+/g

\d matches a single digit, so it will match each digit inidividually, hence multiple results. \d+ matches 1 or more digits, so it will match as many consecutive digits it finds.

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

Comments

0

var re = /\((.+)\)/g;
var str = 'Pages (226)';
var m = re.exec(str);
var element = document.getElementById("answer");
element.innerHTML = m[1];
<div id="answer"></div>

Comments

0

try with method array.join( expression )

var getPages = "Pages (226)";
var getPagesNew = getPages.match(/\d/g).join( "" );

about this method -> here

-hi hi, Saludos :)

Comments

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.