问答详情
源自:2-12 字符串对象方法

怎么匹配List啊,我不会了,请教老师和大家

Welcome to Node.js v12.13.0.

Type ".help" for more information.

> var str="[1   张三    18      2001-01-01, 2   李四    25      1994-01-01]";

undefined

> console.log(str.replace(/[(\d{1})\t(\w{2})\t(\d{1})\t(\w[\d{4}-\d{2}-\d{2}]),]/g,$1$2$3$4));

Thrown:

SyntaxError: Invalid regular expression: /[(\d{1})\t(\w{2})\t(\d{1})\t(\w[\d{4}-\d{2}-\d{2}]),]/: Unmatched ')'

> var str2="[1   张三    18      2001-01-01]";

undefined

> console.log(str.replace(/[(\d{1})\t(\w{2})\t(\d{1})\t(\w[\d{4}-\d{2}-\d{2}])]/g,$1$2$3$4));

Thrown:

SyntaxError: Invalid regular expression: /[(\d{1})\t(\w{2})\t(\d{1})\t(\w[\d{4}-\d{2}-\d{2}])]/: Unmatched ')'

>  

怎么匹配List啊,我不会了,请教老师和大家

提问者:海棠1994 2019-11-30 15:14

个回答

  • 慕勒215309
    2019-12-11 08:26:04

    var str = "[1   张三    18      2001-01-01]";

    var ptn = /\[\d{1,3}\s+[\u4E00-\u9FFF]{2,4}\s+\d{1,3}\s+\d{4}-\d{2}-\d{2}\]/;

    console.log(ptn.test(str));  //true

    备注:编号限1-3位数字,姓名限2-4个汉字,年龄限1-3位数字,[\u4E00-\u9FFF]用于匹配汉字。