Matt
Matt

Reputation: 173

Remove commas from regex javascript

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.

Upvotes: 0

Views: 1010

Answers (3)

Pierre
Pierre

Reputation: 17397

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.

Upvotes: 1

Caxvalencia
Caxvalencia

Reputation: 61

try with method array.join( expression )

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

about this method -> here

-hi hi, Saludos :)

Upvotes: 0

Jeff
Jeff

Reputation: 2423

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>

Upvotes: 0

Related Questions