正則常用的六種方法


正則中有常用的六種方法;分為兩類,一類是:RegExp對象方法;一類是:支持正則表達式的String對象的方法。 

一、RegExp對象方法(兩個):exce()和test()

  語法:RegExpObject.exec(string)

  1.1:exce()方法

    介紹:exec() 方法用於檢索字符串中的正則表達式的匹配,如果字符串中有匹配的值返回該匹配值構成的數組 ,且該數組還有繼承的屬性:

      index:表示第一個匹配的字符在原字符串中的位置,input:表示原字符串,groups:表示當初中命名的分組時匹配到的分組對象;

      exec()方法沒有匹配到數據時返回 null。

  當正則匹配中沒有分組時:

  var str="Hello45647 123 world!Hello 1423 world! ssfsdf";
  var patt1=/\d+/;
  var result1=patt1.exec(str);
  console.log(result1)

 當正則匹配中有分組且分組存在名字時:

var str="Hello45647  123 world!Hello 1423 world! ssfsdf";
var patt2=/(?<hello>\d)+/;
var result2=patt2.exec(str); 
console.log(result2)

  沒有匹配到符合正則的字符時:

var str="Hello   world!Hello  world! ssfsdf";
var patt1=/\d+/;
var result1=patt1.exec(str); 
console.log(result1)//null

  1.2、test()方法

  介紹:方法用於檢測一個字符串是否匹配某個模式;如果字符串中有匹配的值返回 true ,否則返回 false。

var str="Hello world!";
//查找"Hello"
var patt1=/Hello/g;
var result1=patt1.test(str);
console.log(result1); //true
//查找 "Runoob"
var patt2=/Runoob/g;
var result2=patt2.test(str);
console.log(result2); //false

 

二、支持正則表達式的String對象的方法:search()、replace()、split()、match()

語法:string.search(RegExpObject)

  2.1、search()方法

  簡介:用於檢索字符串中指定的子字符串,或檢索與正則表達式相匹配的子字符串。

    如果找到任何匹配的子串,則返回 該子串在原字符串中的第一次出現的位置。  

    如果沒有找到任何匹配的子串,則返回 -1。

var str1 = 'hello 123 world 456';
var reg1 = /\d+/; 
console.log(str1.search(reg1));//6
console.log(str1.search("world"));//10
var str2 = 'hello world';
var reg2 = /\d+/;
console.log(str2.search(reg2));//-1
console.log(str2.search("nihao"));//-1

  2.2、replace()方法    

   簡介:用於在字符串中用一些字符替換另一些字符,或替換一個與正則表達式匹配的子串。原字符串不變,創建一個新的字符串

  創建一個新的字符串,原字符串不變

var str="hello world! hello world! hello world!"; 
var n=str.replace('hello',"Runoob");
console.log(str)
console.log(n)

  替換第一個和替換全部符合正則的子串

var str="hello world! hello world! hello world!"; 
var a=str.replace(/hello/,"Runoob");
var b=str.replace(/hello/g,"Runoob");
console.log(a)
console.log(b)

  2.3、split()方法

   簡介:用於把一個字符串按符合匹配條件的方式分割成一個字符串數組。不改變原字符串

  var str="How 1are 2you 3doing 4today?";
  var a=str.split(" ");
  var b=str.split(" ",2);
  var c=str.split(/\d/);
  var d=str.split(/\d/,3);
  console.log(a);//["How", "1are", "2you", "3doing", "4today?"]
  console.log(b);//["How", "1are"]
  console.log(c);//["How ", "are ", "you ", "doing ", "today?"]
  console.log(d);//["How ", "are ", "you "]

  2.4、match()方法

  簡介:match() 方法可在字符串內檢索指定的值,或找到一個或多個正則表達式的匹配

  注意: match() 方法將檢索字符串 String,以找到一個或多個與 regexp 匹配的文本。這個方法的行為在很大程度上有賴於 regexp 是否具有標志 g。

     如果 regexp 沒有標志 g,那么 match() 方法就只能在 stringObject 中執行一次匹配,與exce的完全一致。

     如果 regexp 有標志 g,它將找到全部符合正則子字符串,並返回一個數組。

     如果沒有找到任何匹配的文本,無論有沒有g,match() 將返回 null。

  沒有g的正則匹配

var str="Hello45647 123 world!Hello 1423 world! ssfsdf";
var patt1=/\d+/;
var result1=str.match(patt1); 
console.log(result1)

 

var str="Hello45647  123 world!Hello 1423 world! ssfsdf";
var patt2=/(?<hello>\d)+/;
var result2=str.match(patt2); 
console.log(result2)

  有g的正則匹配

var str="The rain in SPAIN stays mainly in the plain"; 
var n=str.match(/ain/g);
console.log(n); //["ain", "ain", "ain"]

  沒有匹配到子字符串

var str="The rain in SPAIN stays mainly in the plain"; 
var n=str.match(/\d/g); //匹配數字
console.log(n); //n


 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM