JavaScript中字符串的常用操作方法及特殊字符

下面看下js字符串的常用操作方法,具体内容如下所述:

charat() 获取字符串中特定索引处的字符;

toupperCase() 将字符串的所有字符转换成大写字母;

indexOf() 返回字符串中特定字符串第一次出现的位置

substring() 返回字符串的某个子串

slice() 返回字符串中的某个子串,支持负数参数(字符串中倒数第一个字符定为-1)

concat() 用于将多个字符串拼加成一个字符串

replace() 将字符串中的某个子串以特定的字符串替换

split() 将某个字符串分割成多个字符串,可以指定分隔符

match() 使用正则表达式搜索目标子字符串

search() 使用正则表达式搜索目标子字符串(返回的是索引)

ps:JavaScript 特殊字符

你可以在 JavaScript 中使用反斜杠来向文本字符串添加特殊字符。

插入特殊字符

反斜杠用来在文本字符串中插入省略号、换行符、引号和其他特殊字符。

请看下面的 JavaScript 代码

rush:js;"> var txt="We are the so-called "Vikings" from the north." document.write(txt)

在 JavaScript 中,字符串使用单引号或者双引号来起始或者结束。这意味着上面的字符串将被截为:We are the so-called。

解决这个问题,就必须把在 "Viking" 中的引号前面加上反斜杠 (\)。这样就可以把每个双引号转换为字面上的字符串。

rush:js;"> var txt="We are the so-called \"Vikings\" from the north." document.write(txt)

现在 JavaScript 就可以输出正确的文本字符串了:We are the so-called "Vikings" from the north。

这是另一个例子:

rush:js;"> document.write ("You \& me are singing!")

上面的例子会产生以下输出

You & me are singing!

下面的表格列出了其余的特殊字符,这些特殊字符都可以使用反斜杠来添加到文本字符串中:

normal; border-collapse: collapse; border-bottom: rgb(170,170) 1px solid; text-transform: none; word-spacing: 0px; font-weight: normal; color: rgb(0,0); padding-bottom: 0px; font-style: normal; text-align: left; padding-top: 0px; padding-left: 0px; margin: 15px 0px 0px; border-left: rgb(170,170) 1px solid; orphans: 2; widows: 2; letter-spacing: normal; padding-right: 0px; width: 809px; background-color: rgb(253,252,248); text-indent: 0px; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px"> ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px"> ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(245,245,245)"> ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(255,255)">
ottom: rgb(63,63) 1px solid; color: rgb(255,255,255); padding-bottom: 5px; text-align: left; padding-top: 5px; padding-left: 6px; margin: 0px; border-left: rgb(63,63) 1px solid; padding-right: 15px; background-color: rgb(63,63)">代码 输出 ottom: rgb(170,170) 1px solid; padding-bottom: 6px; padding-top: 6px; padding-left: 6px; margin: 0px; border-left: rgb(170,170) 1px solid; padding-right: 15px">\'ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(245,245)"> ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(255,170) 1px solid; padding-right: 15px">\&ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(245,170) 1px solid; padding-right: 15px">\\ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(255,170) 1px solid; padding-right: 15px">\nottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(245,170) 1px solid; padding-right: 15px">\rottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(255,170) 1px solid; padding-right: 15px">\tottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(245,170) 1px solid; padding-right: 15px">\bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(255,170) 1px solid; padding-right: 15px">\f

总结

以上所述是小编给大家介绍的JavaScript中字符串的常用操作方法,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...