js 正则表达式 的匹配查找,替换,匹配两个特定字符间以前以后的内容

js截取两个字符串之间的内容:     html

var str = "aaabbbcccdddeeefff"; str = str.match(/aaa(\S*)fff/)[1]; alert(str);//结果bbbcccdddeee

js截取某个字符串前面的内容:正则表达式

var str = "aaabbbcccdddeeefff"; tr = str.match(/(\S*)fff/)[1]; alert(str);//结果aaabbbcccddd

js截取某个字符串后面的内容:this

var str = "aaabbbcccdddeeefff"; str = str.match(/aaa(\S*)/)[1]; alert(str);//结果bbbcccdddeeefff

JS利用正则表达式替换字符串中的内容

//从字符串'Is this all there is'中剪去'is':
  var str='Is this all there is'; var subStr=new RegExp('is');//建立正则表达式对象
  var result=str.replace(subStr,"");//把'is'替换为空字符串
  console.log(result);//Is th all there is

  var subStr=new RegExp('is','i');//建立正则表达式对象,不区分大小写
  var result=str.replace(subStr,"");//把'is'替换为空字符串
  console.log(result);//this all there is
    
  var subStr=new RegExp('is','ig');//建立正则表达式对象,不区分大小写,全局查找
  var result=str.replace(subStr,"");//把'is'替换为空字符串
  console.log(result);//th all there 

  var subStr=/is/ig;//直接量法建立正则表达式对象,不区分大小写,全局查找
  var result=str.replace(subStr,"");//把'is'替换为空字符串
  console.log(result);//th all there 
 console.log(str);//Is this all there is 可见replace并不改变原始str

 js  正则 表达式修饰符:

  i   不区分大小写   spa

 g   匹配全局(查找全部匹配而非在找到第一个匹配后中止 ) code

 m 多行匹配  htm

js  正则表达式表达模式:

 一、方括号用于查找某个范围内的字符:对象

[abc]  查找方括号之间的内容blog

[0-9]  查找0到9的数字字符串

(x|y)查找任何以 | 分隔的选项get

二、元字符是拥有特殊含义的字符:

\d 查找数字

\s 空白字符

\b 单词边界

\uxxxx  查找以十六进制数 xxxx 规定的 Unicode 字符。

三、量词

n+ 匹配任何包含至少一个 n 的字符串。

n*  匹配任何包含零个或多个 n 的字符串。

n? 匹配任何包含零个或一个 n 的字符串。

regExp 对象

预约义匹配属性和方法    new regExp()

相关文章
相关标签/搜索