The morning master left me a task, let's think about how to use regular expressions, find numbers in strings, and replace numbers. The following code uses a regular expression to find a number in a string, replacing the original number with the twice-fold value of the found number using the Replace () method. The second parameter of the replace () method is a function that returns twice times the value of the found number.
<script>
var str= "Last year was 2014, this year is 2015";
var function () {
// This and arguments are generated internally when the method is called Console.log (arguments[0]);
// after you find a number, you can do other things with the number return arguments[0] * 2; });
Console.log (NEWSTR);
</script>
Regular expression/\d+/g: matches at least one number.
Arguments,console.log (arguments) is generated internally when the function is called; The result:
The numbers 2014 and 2015 need to be obtained, so just get arguments[0].
Console.log (Arguments[0]); The result:
The final result is: "Last year was 4028, this year is 4030"
After you find a number, you can also do other things with numbers
JavaScript Regular Expressions: finding numbers in strings