JavaScript Regular Expressions: finding numbers in strings

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.