Regular expressions are used in JavaScript

Source: Internet
Author: User
Remove unnecessary spaces at the beginning and end of the string. g is the full text search for all matching functionString. prototype. Trim () {returnthis. repl remove unnecessary spaces at the beginning and end of the string.
/G is the full text search for all matching function strings. prototype. trim () {return this. replace (/(^ \ s *) | (\ s * $)/g, "");} function String. prototype. LTrim () {return this. replace (/(^ \ s *)/g ,"");}


Function String. prototype. RTrim () {return this. replace (/(\ s * $)/g ,"");}

--------------------------------------------------------------
Application: Calculate the length of a string (two-byte length Meter 2, ASCII character meter 1)

String. prototype. len = function () {return this. replace ([^ \ x00-\ xff]/g, "aa"). length ;}

--------------------------------------------------------------
Application: javascript does not have trim functions like vbscript. We can use this expression to implement it, as shown below:

String. prototype. trim = function ()
{
Return this. replace (/(^ \ s *) | (\ s * $)/g ,"");
}
Javascript programs that extract file names from URLs using regular expressions. the following result is page1.

S = "http://www.9499.net/page1.htm"
S = s. replace (/(. * \/) {0,} ([^ \.] +). */ig, "$2 ")
Alert (s)

# Use regular expressions to restrict text box input in a webpage form:

--------------------------------------------------------------
You can only enter Chinese characters using regular expressions: onkeyup = "value = value. replace (/[^ \ u4E00-\ u9FA5]/g, ') "onbeforepaste =" clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ u4E00-\ u9FA5]/g ,'))"

--------------------------------------------------------------
You can only enter the full-width characters: onkeyup = "value = value. replace (/[^ \ uFF00-\ uFFFF]/g, ') "onbeforepaste =" clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ uFF00-\ uFFFF]/g ,'))"

--------------------------------------------------------------
Use a regular expression to limit that only numbers can be entered: onkeyup = "value = value. replace (/[^ \ d]/g, ') "onbeforepaste =" clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ d]/g ,'))"

--------------------------------------------------------------
You can only enter numbers and English letters using regular expressions: onkeyup = "value = value. replace (/[\ W]/g, ') "onbeforepaste =" clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ d]/g ,'))"

Related Article

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.