1、js截取两个字符串之间的内容:
// 截取()(中文)之间的内容
let str = "张三(LGJF001)";
const mm = str.match(/((\S*))/)
str = mm[1];
console.log(mm);
console.log(str);//结果LGJF001
结果:
[
'(LGJF001)',
'LGJF001',
index: 2,
input: '张三(LGJF001)',
groups: undefined
]
'LGJF001',
// 截取()(中文)之间的内容
const str1 = "ZHANGSAN(LGJF001)";
const str2 = str1.match(/\((\S*)\)/)[1];
console.log(str2);//结果LGJF001
2、js截取某个字符串(中文左括号)后面的内容:
let str = "张三(LGJF001)";
str = str.match(/((\S*)/)[1];
console.log(str);//结果 LGJF001)
3、js截取某个字符串(中文右括号)前面的内容:
let str = "张三(LGJF001)";
str = str.match(/(\S*))/)[1];
console.log(str);//结果 张三(LGJF001
PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:
JavaScript正则表达式在线测试工具: http://tools./regex/javascript
正则表达式在线生成工具: http://tools./regex/create_reg
更多关于JavaScript相关内容感兴趣的读者可查看专题:
《JavaScript正则表达式技巧大全》、《JavaScript替换操作技巧总结》