JavaScript string manipulation

Source: Internet
Author: User

1. Replacing substrings

Replace (search,replacement)
Find the substring that satisfies the criteria search, replace with replacement

Note: By default, only the first substring found is replaced, and if you want to replace all, you need to write:

 x.replace (/a/g,  o    var  regs = new  RegExp ( a   ", "  g   "; X.replace (regs,   " o  ); 

<script>
function P (s) {
document.write (s);
document.write ("<br>");
}

var x = new String ("Hello JavaScript");
P (' This is the original string: ' +x ');
var y = x.replace ("A", "O");
P (' Replace only the first A: ' +y);
var regs = new RegExp ("A", "G");
var z = x.replace (regs, "O");
P (' Replace all A: ' +z);

</script>

2. Intercept a substring

SUBSTRING intercept a substring
Note: The second parameter, cannot be taken

Cases:

< script >    var x = new String("Hello JavaScript");   document.write( ‘字符串x的值: ‘+x); document.write(‘< br >‘); document.write(‘x.substring (0,3) 获取位0到3的字符串: ‘+x.substring (0,3) ); document.write(‘< br >‘); document.write(‘左闭右开,取得到0,去不到3‘); </ script >3. Returns the character at the specified position

CharAt returns the character at the specified position
charCodeAt returns the Unicode code corresponding to the character at the specified position

< script >   var y = new String("Hello JavaScrpt"); document.write("字符串y的值:"+y); document.write("< br >"); document.write(‘通过 charAt(0)获取位置0的字符串: ‘+y.charAt(0)); //返回H document.write("< br >"); document.write(‘通过 charCodeAt(0)获取位置0的字符的 Unicode码 :‘+y.charCodeAt(0)); //返回H对应的Unicode码 72 </ script >

JavaScript string manipulation

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.