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 ,'))"