More than 200 JS tricks code (VI) __JS

Source: Internet
Author: User
169. Another way to dynamically modify the CSS This.runtimeStyle.cssText = "color: #990000; border:1px solid #cccccc";//170. Regular expression matches a regular expression of a Chinese character: [\ U4E00-\U9FA5] matches double-byte characters (including Chinese characters): [^\x00-\xff] Apply: Compute the length of a string (a double-byte character length meter 2,ascii character 1) string.prototype.len=function () {
 
Return This.replace ([^\x00-\xff]/g, "AA"). Length;} A regular expression that matches a blank row: \n[\s|] *\r regular expression that matches the HTML tag:/< (. *) >.*<\/\1>|< (. *) \/>/matches a regular expression with a trailing space: (^\s*) | (\s*$) Application: JavaScript does not have a trim function like VBScript, we can use this expression to implement, as follows: String.prototype.trim = function () {return this.rep Lace (/(^\s*) | (
 
\s*$)/g, ""); Using regular expressions to decompose and transform IP addresses: The following is a JavaScript program that uses regular expressions to match an IP address and converts an IP address to a corresponding numeric value: function IP2V (IP) {re=/(\d+) \. ( \d+) \. (\d+) \. (\d+)/g//matching IP address regular expression if (Re.test (IP)) {return Regexp.$1*math.pow (255,3)) +regexp.$2*math.pow (255,2)) +regexp.$3*255
 
+regexp.$4*1} else {throw new Error ("not a valid IP address!")}} However, if the above program does not use regular expressions, and the split function to decompose directly can be simpler, the program is as follows: Var ip= "10.100.20.168" Ip=ip.split (".") Alert ("IP value is:" + (ip[0]*255*255* 255+ip[1]*255*255+ip[2]*255+ip[3]*1) A regular expression matching an email address: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) * Matching the URL of the regular expression: http://([\w-]+\.)
 
+[\w-]+ (/[\w-/?%&=]*)? An algorithmic program that uses regular expressions to remove repetitive characters from a string: var s= "Abacabefgeeii" var s1=s.replace (/(.). *\1/g, "$") var re=new RegExp ("[" +s1+ "]", "G") var s2=s.replace (Re, "") alert (S1+S2)//Result: ABCEFGI The idea is to use a back reference to take out the characters that contain duplicates, and then to A duplicate character creates a second expression, takes a distinct character, and concatenates the two.
 
This method may not apply to strings that are required for character order. A JavaScript program that extracts a filename from a URL address with a regular expression is the following result for Page1 s= "http://www.9499.net/page1.htm" S=s.replace (/(. *\/) {0,} ( [^\.] +). */ig, "$") alert (s)/////////use regular expressions to restrict text boxes in a Web page form input: Restrict only Chinese in regular expressions: onkeyup= "Value=value.replace" (/[^\u4e00-\ u9fa5]/g, "onbeforepaste=" clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\u4e00-\ u9fa5]/g, ') ' restricts only full-width characters in regular expression: onkeyup= "Value=value.replace (/[^\uff00-\uffff]/g,") "onbeforepaste="
 
Clipboarddata.setdata (' Text ', Clipboarddata.getdata (' text '). Replace (/[^\uff00-\uffff]/g, ') "
 Only numbers can be entered with regular expression restrictions: onkeyup= "Value=value.replace (/[^\d]/g,")
"Onbeforepaste=" clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') " Only numbers and English can be entered with regular expression restrictions: onkeyup= "Value=value.replace (/[\w]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ',
Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') "171. Set up and use cookies <HTML> <BODY> settings with read cookies ... The value written to the cookie <input type=text name=gg> <input type = button value = "Set cookie" OnClick = "set ()" > <input type = Button Value = "Read cookie" OnClick = "Get ()" > <input TYPE = TEXT NAME = textbox> </BODY> <script Langua  Ge= "JavaScript" > Function Set () {var Then = new Date () Then.settime (Then.gettime () + 60*1000)//60 seconds Document.cookie =
 "Cookie1=" +gg.value+ "; expires=" + then.togmtstring ()} function get () {var cookiestring = new String (document.cookie) var cookieheader = "Cookie1=" var beginposition = Cookiestring.indexof (Cookieheader) if (beginposition!=-1) {docu Ment.all.Textbox.value = cookiestring.substring (beginposition + COokieheader.length)} else Document.all.Textbox.value = "Cookie not Found!"}
 </SCRIPT>  


 

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.