js regular expression, match content in parentheses (excluding parentheses)

Case 1: JS regular expression to get the content inside the parentheses and curly braces (including parentheses)

var str = "12【开始】3{xxxx}456[我的]789123[你的]456(1389090)78【结束】9";

var regex1 = /\((.+?)\)/g;   // () 小括号
var regex2 = /\[(.+?)\]/g;   // [] 中括号
var regex3 = /\{(.+?)\}/g;  // {} 花括号,大括号
var regex4 = /\【(.+?)\】/g;  // {} 中文大括号

// 输出是一个数组
console.log(str.match(regex1)); // ["(1389090)"]
console.log(str.match(regex2)); // ["[我的]", "[你的]"]
console.log(str.match(regex3)); // ["{xxxx}"]
console.log(str.match(regex4)); // ["【开始】", "【结束】"]

Regex to match characters within parentheses, excluding parentheses

(?<=\()\S+(?=\))

(?<=exp) is a string that starts with exp, but does not contain itself.
(?=exp) matches a string that ends with exp, but does not contain itself.

(?<=() That is, start with a parenthesis, but do not contain parentheses.

(?=)) is to end with parentheses

\S matches any non-whitespace character. Equivalent to [^\f\n\r\t\v].
+ indicates at least one character.

(?<=()\S+(?=)) is a string that matches at least one non-blank character in parentheses ending with (beginning, ), but not including the beginning (and end)

Case 2 is based on Case 1 without parentheses

var str = "12【开始】3{xxxx}456[我的]789123[你的]456(1389090)78【结束】9";

var regex1 = /(?<=\()(.+?)(?=\))/g;   // () 小括号
var regex2 = /(?<=\[)(.+?)(?=\])/g;   // [] 中括号
var regex3 = /(?<=\{)(.+?)(?=\})/g;  // {} 花括号,大括号
var regex4 = /((?<=\【)(.+?)(?=\】))/g;  // {} 中文大括号

// 输出是一个数组
console.log(str.match(regex1)); // ["1389090"]
console.log(str.match(regex2)); // ["我的", "你的"]
console.log(str.match(regex3)); // ["xxxx"]
console.log(str.match(regex4)); // ["开始", "结束"]

Guess you like

Origin blog.csdn.net/u013299635/article/details/125717591