Quick problem. Extracting numbers from a string.
4 answers
Use regular expressions : -
var test = "javascript:change(5);"
var number = new RegExp("\\d+", "g")
var match = test.match(number);
alert(match);
+1
source to share
A simple RegExp can solve this problem:
var inputString = 'javascript:change(5);';
var results = /javascript:change\((\d+)\)/.exec(inputString);
if (results)
{
alert(results[1]); // 5
}
Using a part javascript:change
in a match also ensures that if the string is not in the correct format, you don't get a value from the matches.
+1
source to share