我是否遗漏了基本node.js正则表达式语法中的内容?
如果我以^开头或以$结尾无关紧要,我在正则表达式语法中缺少什么来获取这些输出:
var Regex = require("regex");
//var Regex = require("regexp"); //performs the same with both
var regex = new Regex(/(a|b)*abb/);
console.log("The pattern matches: " + regex.test("abb")); // true
//Match a decimal number in sqaure brackets
var regex1 = new Regex(/\d/);
console.log("The pattern matches: " + regex1.test("9"));
var regex2 = new Regex(/^\d$/);
console.log("The pattern matches: " + regex2.test("9"));
var regex3 = new Regex(/^\d$/);
console.log("The pattern matches: " + regex3.test("9"));
var regex4 = new Regex(/^\[\d\.\d\]$/);
console.log("The pattern matches: " + regex4.test("[9.9]"));
var regex5 = new Regex(/^\[[0-9]\.[0-9]\]$/);
console.log("The pattern matches: " +regex5.test("[9.9]"));
输出:
C:\nodeapps\CSV IO>node regexTest.js
The pattern matches: true
The pattern matches: false
The pattern matches: false
The pattern matches: false
The pattern matches: false
The pattern matches: false
我在语法上没有看到什么吗?
答案 0 :(得分:1)
我们不需要(“正则表达式”); 只需删除它并使用。默认的javascript RegExp它可以工作。
修改后的代码
var regex = new RegExp(/(a|b)*abb/);
console.log("The pattern matches: " + regex.test("abb")); // true
//Match a decimal number in sqaure brackets
var regex1 = new RegExp(/\d/);
console.log("The pattern matches: " + regex1.test("9"));
var regex2 = new RegExp(/^\d$/);
console.log("The pattern matches: " + regex2.test("9"));
var regex3 = new RegExp(/^\d$/);
console.log("The pattern matches: " + regex3.test("9"));
var regex4 = new RegExp(/^\[\d\.\d\]$/);
console.log("The pattern matches: " + regex4.test("[9.9]"));
var regex5 = new RegExp(/^\[[0-9]\.[0-9]\]$/);
console.log("The pattern matches: " +regex5.test("[9.9]"));