正则表达式 中,如何匹配字符串中的 ''

Posted 弋儿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了正则表达式 中,如何匹配字符串中的 ''相关的知识,希望对你有一定的参考价值。

‘ \ ‘ 作为特别功能的转义字符,在正则表达式有举足轻重的重要

‘ \ ‘ 在字符串中也具有让某些普通字符变得不普通的能力,比如 ‘ \t ‘, ‘ \n ‘ 这些众所周知的;当 ‘\’ + 某个字符 不具有特殊功能时,加不加其实都没差别

var str = ‘he\tllo‘;
console.log(str);                // he    llo

 如何匹配字符串中的 ‘ \ ‘ 呢?( 单个 ‘\‘ 其实是无法被匹配的 )

1、确定真实的字符串 

2、写正则表达式

举个栗子:

 1 var str = ‘he\llo‘;
 2 console.log(str);                // hello
 3 
 4 var reg = /he\\llo/;
 5 console.log( reg.exec(str) );    // null
 6 
 7 reg = /hello/;
 8 console.log( reg.exec(str) );    
 9 reg = new RegExp(‘hello‘);
10 console.log( reg.exec(str) );    // ["hello", index: 0, input: "hello"]
11 
12 reg = /\h\e\l\l\o/;
13 console.log( reg.exec(str) );    // ["hello", index: 0, input: "hello"]
14 
15 
16 
17 str = ‘he\\llo‘;
18 console.log(str);                // he\llo
19 
20 reg = /he\\llo/;
21 console.log( reg.exec(str) );    
22 reg = new RegExp(‘he\\\\llo‘);
23 console.log( reg.exec(str) );    // ["he\llo", index: 0, input: "he\llo"]

 

以上是关于正则表达式 中,如何匹配字符串中的 ''的主要内容,如果未能解决你的问题,请参考以下文章

如何使用正则表达式删除字符串中的括号符号

如何用正则表达式匹配括号中的内容,不包含括号

oracle10g中,如何查询正则表达式匹配指定字符串的匹配个数?

如何用正则表达式匹配括号中的内容。

js 正则表达式,字符串中如何匹配id的内容?

说说在 Python 中,如何找出所有字符串匹配