Jquery processes strings

Source: Internet
Author: User

1. Remove spaces.

   txt=$.trim($("txt1").val());


2. convert it to a number.

 txtNum=Number($.trim(txt)) + 1  thisEle = $("#para").css("font-size");   textFontSize = parseFloat(thisEle , 10); 

 


3, rounded to integer/Random Number

           Math.ceil(4.8992303)     Math.floor(4.8992303)    Math.round(4.8992303)    Math.ceil(4.29993354)    Math.floor(4.29993354)   Math.round(4.29993354)    Math.round(Math.random()*100); 

 


4. truncate a string

  txt=$("p").text().substr(0,15);

 


5. String replacement

  $("image").attr("src").replace("size=60", "size=200"   $("#txt").replace(/[^\d-]/g, "").replace(/^\-/g, ""); 

 


6. Split the string

Str = arr = str = "Baidu, Farmer it station, Google, bamboo style, nongfuit.com, webpage chat group, 180550045 welcome to" arr = str. split (','); (I = 0; I <arr. length; I ++}

 


7. Mutual conversion between js and jquery objects

  aa = $("#mm").get(0);   bb = $(aa);  

 


8. Use Regular Expression matching

MatchTel =/^ ([0 \ +] \ d {2, 3 }-)? (0 \ d {2, 3})-) (\ d {7, 8}) (-(\ d {3 ,}))? $ /(! MatchTel. test ($ ("# txtTel" alert ("Incorrect phone format! "! 1}

 

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.