Reputation: 173
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
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
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
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