在 JavaScript 中,能夠經過兩種方式建立一個正則表達式。正則表達式
方式一:經過調用RegExp對象的構造函數建立 函數
var regexp = new RegExp(/123/);
console.log(regexp);
方式二:利用字面量建立 正則表達式測試
var rg = /123/;
test() 正則對象方法,用於檢測字符串是否符合該規則,該對象會返回 true 或 false,其參數是測試字符串。spa
var rg = /123/;
console.log(rg.test(123));//匹配字符中是否出現123 出現結果爲true
console.log(rg.test('abc'));//匹配字符中是否出現123 未出現結果爲false