本文列舉了利用正規表達式比對字元串,取出該字元串中想要的内容
1、取出小括号裡的内容
/**
* 取出小括号裡的内容
*/
function getParenthesesStr(text) {
var result = '';
if (isNull(text))
return result;
var regex = /\((.+?)\)/g;
var options = text.match(regex);
if (!isNull(options)) {
var option = options[0]
if (!isNull(option)) {
result = option.substring(1, option.length - 1);
}
}
return result;
}
2、取出分号和換行之間的内容
/**
* 取出分号和換行之間的内容
*/
function getStr(text) {
var result = '';
if (isNull(text))
return result;
var regex = /\;(.+?)\\n/g;
var options = text.match(regex);
if (!isNull(options)) {
var option = options[0]
if (!isNull(option)) {
result = option.substring(1, option.length - 1);
}
}
return result;
}
在使用過程中,可根據你指定的比對字元進行更改正規表達式
如有問題,歡迎評論區留言~