说说正则表达式

正则表达式

  1. var a=/\d/g;
  2. //a 的属性
  3. global: true
  4. ignoreCase: false
  5. lastIndex: 0
  6. multiline: false
  7. source: "\d"

关于正则表达式的一些表示方法这里不做说明....

正则表达式的方法

compile方法

....实在想不通为啥要这个方法

test方法

检测给定的字符串是否符合该正则表达式规则。

test的用法

参数为字符串,如果匹配,返回true,否则返回false。

  1. var reg2=/\d/g;
  2. var r=reg2.test('2349efjo');
  3. r
  4. //true
  5. RegExp.lastMatch
  6. //"2"
  7. RegExp.lastParen
  8. //""
  9. RegExp.input
  10. //"2349efjo"
  11. RegExp.leftContext
  12. //""
  13. RegExp.rightContext
  14. //"349efjo"
  15. RegExp.mutiline
  16. //false
结论
  • 全局的RegExp对象在匹配后会带上一些属性。
    • input: 对字符串参数的引用。
    • lastMatch: 最近一次的匹配项
    • leftContext: lastMatch左边的内容
    • rightContext: lastMatch右边的内容
    • lastParen: 最近一次匹配的捕获组,没有捕获组则为空
    • multiline: 是否所有表达式都使用了多行模式,如果是则为true, 否则为false。
    • 19: 用于匹配对应的捕获组。

exec方法

执行匹配,返回一个匹配的数组,如果没有匹配上则返回null。

exec用法
  1. var r=/(\w+)(\d+)-/g;
  2. r.exec('efefj2193902-84')
  3. //["efefj2193902-", "efefj219390", "2"]
  4. r.lastIndex
  5. //13
  6. r.exec('ejfiefjjeo213487329487294235897498327948-')
  7. //["487329487294235897498327948-", "48732948729423589749832794", "8"]
  8. r.lastIndex
  9. //41
  10. r.lastIndex=0
  11. r.exec('ejfiefjjeo213487329487294235897498327948-')
  12. //["ejfiefjjeo213487329487294235897498327948-", "ejfiefjjeo21348732948729423589749832794", "8"]
  13. RegExp.lastMatch
  14. //"ejfiefjjeo213487329487294235897498327948-"
  15. RegExp.leftContext
  16. //""
  17. RegExp.rightContext
  18. //""
  19. RegExp.lastParen
  20. //"8"
  21. RegExp.multiline
  22. //false
  23. RegExp.input
  24. //"ejfiefjjeo213487329487294235897498327948-"
结论
  • 返回的数组的项依次为[匹配整个正则的字符串,匹配第一个子表达式的字符串,匹配第二个子表达式的字符串......]
  • 在执行exec的过程中, 正则表达式的lastIndex方法记录了匹配到的字符串的最后一个索引后面的位置(即索引+1).
  • lastIndex是可以重新被赋值的,如果后面还有字符串要匹配,要把lastIndex重置为0。
  • 执行exec对全局RegExp对象的影响,属性在test时已经介绍过。

和正则表达式相关的字符串方法

match方法

match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置。

match方法的使用

其参数是一个字符串或正则表达式.
找出匹配指定参数的字符串并组成数组返回
数组有两个属性,分别是index和input属性
index属性指定了匹配字符的起始字符在原字符串中的索引,input是对原字符串的引用。

  1. var ex=/\d*/g;
  2. '1221343'.match(ex)
  3. //["1221343", ""]
  1. var ex=/do/g;
  2. var str="doesesdoes";
  3. str.match(ex);
  4. //['do','do'];
  5. str.match(ex).index
  6. //undefined
  7. str.match(ex).input
  8. //undefined
  1. var str='helloworld';
  2. srt.match('o');
  3. //['o']
  4. str.match('o').index;
  5. //4
  6. str.match('o').input;
  7. //helloworld
  1. var str='helloworld';
  2. str.match(/o/);
  3. //["o"]
  4. str.match(/o/).index
  5. //4
  6. str.match(/o/).input
  7. //"helloworld"
  1. 'efhu'.match('1')
  2. //null
  3. 'efhu'.match('1').index
  4. //VM1036:2 Uncaught TypeError: Cannot read property 'index' of null
  5. 'efhu'.match('1').input
  6. //VM1041:2 Uncaught TypeError: Cannot read property 'input' of null
结论:
  • 返回数组是任意匹配到指定规则的字符串,当然也包括原字符串本身。
  • 全局匹配时,会找出所有符合条件的字符串,而非全局匹配只能得到最早的匹配。
  • 全局匹配时,返回单纯的数组,数组中不包含index和input属性。而在非全局匹配或字符串匹配时,包含这两个属性。
  • 在匹配不到任意字符串时,得到null,且不再含有index和input属性。这应该是由于无法对null设置属性的原因。注:一般的数组上是可以设置属性的。

search方法

search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。
返回第一个与指定规则匹配的字符串的位置,如果不能匹配则返回-1。

search方法的使用

参数为字符串或正则表达式

  1. 'fjejfi'.search('j');
  2. //1
  3. 'fjejfi'.search('h')
  4. //-1
  5. 'hellowh'.search('h')
  6. //0
  7. 'hellowh'.search(/h/g)
  8. //0
结论
  • search方法在匹配不到字符时返回-1。
  • search方法不care是不是全局匹配,它只负责第一次匹配。

replace方法

replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。

replace用法

参数是(字符串/正则表达式,字符串/函数)。

  1. 'efejiejfief'.replace('e','4')
  2. //"4fejiejfief"
  3. 'efejiejfief'.replace(/e/g,'4')
  4. //"4f4ji4jfi4f"
  5. '2014-08-09'.replace(/(\d+)-(\d+)-(\d+)/,'$2/$3/$1')
  6. //"08/09/2014"
  7. '2014-08-09'.replace(/(\d+)-(?:\d+)-(\d+)/,'$2/$3/$1')
  8. "09/$3/2014"
  9. '122233434'.replace('\\d+','h')
  10. //"122233434"
  11. 'helloword'.replace('o',function(v){return '$'+v+'$'});
  12. //"hell$o$world"
  13. 'helloworld'.replace('o',function(v,j){return '$'+j+'$'})
  14. //"hell$4$world"
  15. 'helloworld'.replace('o',function(v,j,a){return '$'+a+'$'})
  16. //"hell$helloworld$world"
  17. 'helloworld'.replace(/o/g,function(v,j,a){return '$'+v+'$'})
  18. //"hell$o$w$o$rld"
  19. 'helloworld'.replace(/o/g,function(v,j,a){return '$'+a+'$'})
  20. //"hell$helloworld$w$helloworld$rld"
  21. 'helloworld'.replace(/o/g,function(v,j,a){return '$'+j+'$'})
  22. //"hell$4$w$6$rld"
结论
  • 如果是两个字符串,那么replace只会替换第一个匹配的字符
  • 如果第一个参数是一个全局匹配的正则,那么replace将全部替换匹配的字符。
  • 在replace方法中,我们可以使用12、3...n这样的替代符。
  • 当前面一个参数是正则字符串时,replace并不会识别它为正则表达式,也不会对其进行转换,而是当作普通的字符串。
  • 对于replace的第二个参数,如果是一个函数的话,那么函数的参数依次是匹配的字符串、所在位置、原字符串。

split方法

用于根据指定规则切分字符串成数组

split用法

参数为(字符串[,howmany])或(正则[,howmany])

  1. 'one1two2three3four4'.split(/\d/)
  2. //["one", "two", "three", "four", ""]
  3. 'helloworld'.split('o')
  4. //["hell", "w", "rld"]
  5. 'helloworld'.split('o',2)
  6. //["hell", "w"]
  7. 'helloworld'.split('o',6)
  8. //["hell", "w", "rld"]
  9. 'helloworld'.split('')
  10. //["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"]
  11. helloworld'.split(/(\w)/)
  12. //["", "h", "", "e", "", "l", "", "l", "", "o", "", "w", "", "o", "", "r", "", "l", "", "d", ""]
  13. 'helloworld'.split(/\w/)
  14. //["", "", "", "", "", "", "", "", "", "", ""]
  15. 'helloworld'.split(/(\w+)/)
  16. //["", "helloworld", ""]
  17. 'helloworld'.split(/\w+/)
  18. //["", ""]
结论
  • 可以用第二个参数指定被切分后的最大数组长度。
  • 如果正则中包含子表达式,那么结果数组中也将包含子表达式匹配的字符串。




posted @ 2015-04-05 23:18  rubyisapm  阅读(391)  评论(0编辑  收藏  举报