对字符,字符串的相关操作--js,jQuery
概述
1、字符方法
charAt()和charCodeAt(),这两个方法都接收一个参数,及基于0的字符位置,charAt()返回的是给定位置的字符,charCodeAt()返回的是给定位置字符的字符编码。与charCodeAt()对应的一个方法是formCharCode()将字符编码转为字符串。
如:
<code class="hljs xml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">script</span>></span><span class="javascript" style="box-sizing: border-box;"> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> code = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"future"</span>; console.log(code.charAt(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//t</span> console.log(code.charCodeAt(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//116</span> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">script</span>></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
除了以上方法外,还可以通过方括号去访问特定位置的字符,如:
<code class="hljs xml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">script</span>></span><span class="javascript" style="box-sizing: border-box;"> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> code = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"future"</span>; console.log(code[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>]); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//t</span> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">script</span>></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
使用方括号的方法在IE8,Firefox,Safari,Chrome和Opera中都可以支持,但是在IE7及更早版本中不支持,返回的值为undefined.
2、字符串操作方法
1、字符串拼接:concact()方法。
<code class="hljs rsl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">var a = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"my "</span>; console.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">log</span>(a.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">concat</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"future"</span>));<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//my future</span> console.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">log</span>(a.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">concat</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"future "</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"will"</span>));<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//my future will</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
2、基于子字符串创建新字符串:slice()、substr()和substring().这三个方法都返回操纵字符串的一个子字符串,而且都接收一到两个参数。
第一个参数指定字符串的开始位置,第二个参数(可省略)表示子字符串到哪里结束。slice()和substring()的第二个参数指定的是子字符串最后一个字符后面的位置,而substr()的第二个参数指定的是返回的字符个数。如果省略第二个参数,则截取到字符串最后一个位置。
<code class="hljs rust has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">var <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"We are family"</span>; console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.slice(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// ar(从第2个位置开始,不包含第5个位置)</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.slice(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//e are family(从第1个位置开始,一直到最后)</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.substring(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//are family(从第3个位置开始,一直到最后)</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.substring(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>));<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//ar(从第3个位置开始,不包含第5个位置)</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.substr(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// are family(从第2个位置开始,一直到最后)</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.substr(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// are f(从第2个位置开始,截取6个字符)</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>
slice()和substring()方法在正值情况下,实现的功能一致,但是在传递的值为负值时,行为就不相同了。
slice()方法会将传入的负值与字符串的长度相加(因此,如果两个参数都会负值,第二个参数必须要大于第一个参数,否则返回的是空字符串)
substr()方法会将负的第一个参数加上字符串的长度,将将第二个参数转换为0。(如果第二个参数为负值,返回空的字符串)
substring()方法将所有的负值参数都转换为0,substring始终会将较小的参数作为开始位置。
<code class="hljs rust has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">var <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Wearefamily"</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//length为11</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.slice(-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>));<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//相当于str.slice(8,3)//返回空字符串</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.slice(-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>,-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>));<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//相当于str.slice(6,10)//返回ami</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.substring(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>));<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//相当于str.substring(3,0)//返回Wea</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.substring(-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>));<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//相当于str.substring(0,0)//返回空字符串</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.substr(-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>));<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//相当于substr(7,3)//返回mil</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.substr(-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>,-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>));<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//相当于substr(7,0)//返回空字符串</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>
3、字符串的位置方法
字符串的位置方法:indexOf()和lastIndexOf()。这两个方法都是从一个字符串中搜索给定的子字符串,然后返回子字符串的位置,没有找到,返回-1.indexOf()从字符串的开头向后搜索子字符串,而lastIndexOf()从字符串的末尾向前搜索子字符串。
<code class="hljs rust has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">var <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"WEAREFAMILY"</span>; console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.indexOf(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"A"</span>));<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.lastIndexOf(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"A"</span>));<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//6</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
4.trim()方法
trim()用于去掉字符串收尾的空格
5、字符串大小写转换方法
转换为大写:toUpperCase()和toLocalUpperCase()
转换为小写:toLowerCase()和toLocalLowerCase()
6、字符串的模式匹配
match(),replace(),search(),replace()以及split()方法是String类型定义的集中用于在字符串中匹配模式的几种方法。
match方法只接收一个参数,要么是一个正则表达式,要么是一个RegExp对象。在字符串上调用match()方法,本质上和调用RegExp的exec()方法相同,返回的是一个数组
<code class="hljs rust has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">var <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"we are weamy"</span>; var pattern = /we/; var matches = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">match</span>(pattern); console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(matches);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//["we", index: 0, input: "we are weamy"]</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(pattern.exec(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>));<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//["we", index: 0, input: "we are weamy"]</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(matches.index);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//0</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(matches[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>]);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//we</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>
search()的参数与match()相同,不同在于search()返回的是字符串中第一个匹配项的索引;如果没有找到匹配项,返回-1.
replace()方法,接收2个参数,第一个参数可以是一个RegExp对象或者是一个字符串,第二个参数是一个字符串或者是一个函数,如果第一个参数是字符串,那么只会替换第一个字符串,如果想要替换所有的字符串,那么必须使用正则表达式,并指定全局标志(g)
<code class="hljs rust has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">var <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"cat, fat, sat, bat"</span>; console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.replace(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"at"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ooo"</span>));<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//cooo, fat, sat, bat</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.replace(/at/g,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ooo"</span>));<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//cooo, fooo, sooo, booo</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
如果replace()的第二个参数是字符串,还可以使用一些特殊的字符序列,将正则表达式操作得到的值插入到结果字符中。
$$ $
S& 匹配整个模式的子字符串
Sn 匹配第n个捕获组的子字符串
<code class="hljs rust has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">var <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"cat, fat, sat, bat"</span>; console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.replace(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"at"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ooo"</span>));<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//cooo, fat, sat, bat</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.replace(/(.at)/g,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ooo ($1)"</span>));<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//ooo (cat), ooo (fat), ooo (sat), ooo (bat)</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.replace(/(.at)/g,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ooo ($&)"</span>));<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//ooo (cat), ooo (fat), ooo (sat), ooo (bat)</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
split()方法接收一个参数作为分隔符,将字符串转变为数组。
与之相对应的方法是join(),用作将数组转变为字符串。
7、LocalCompare()方法
这个方法用于字符串的比较。
如果字符串在字母表中应该拍照字符串参数之前,则返回一个负数。(大多数情况下是-1,具体的值要视实现而定)
如果字符串等于字符串参数,则返回0.
如果字符串在字母表中的顺序排在字符串参数之后,则返回一个正数。(大多数情况下是1,具体的值要视实现而定)
<code class="hljs rust has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">var <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"break"</span>; console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.localeCompare(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"future"</span>)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//-1</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.localeCompare(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"above"</span>)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//1</span> console.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>.localeCompare(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"break"</span>)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//0</span></code>
最后
以上就是舒服鸵鸟为你收集整理的对字符,字符串的相关操作--js,jQuery的全部内容,希望文章能够帮你解决对字符,字符串的相关操作--js,jQuery所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
- 本文分类:jQuery
- 浏览次数:63 次浏览
- 发布日期:2023-10-24 08:00:22
- 本文链接:https://www.kaopuke.com/article/k-p-k_13_u_23_o_22_f1_14_jk3.html
发表评论 取消回复