regex 正则表达式,用于匹配括号内包含字符串的字符串

bfhwhh0e  于 2023-06-30  发布在  其他
关注(0)|答案(1)|浏览(82)

我想创建一个符合特定规则的正则表达式。
我发现了这个RE -/^.*\(.*\).*$/g,但我也想验证括号内的第二个字符串。
例如

United Kingdom (English)

 Belgium (Dutch)

ie-[string space string inside parenthesis with at least 3 length of inside string.]的表达式。
先谢谢你。

esyap4oy

esyap4oy1#

JavaScript中:

const pattern = /^[A-Za-z\s]+\([A-Za-z]{3,}\)$/;
const str1 = 'United Kingdom (English)';
const str2 = 'Belgium (Dutch)';

console.log(pattern.test(str1)); // true
console.log(pattern.test(str2)); // true

模式说明:

^ asserts the start of the string.
[A-Za-z\s]+ matches one or more letters or spaces.
\( matches the opening parenthesis.
[A-Za-z]{3,} matches three or more letters (assumed to be the inside string you mentioned).
\) matches the closing parenthesis.
$ asserts the end of the string.

相关问题