我试图在字符之间的字符串中获取一个值,但我在编写 RegExp 方面有点缺乏经验。我会很高兴得到任何帮助。
我Hello ___world___
从她那里得到了一只独脚金,我需要得到_world_
。world
可以是任何其他值。例如:
Hello ______
-> 应该返回 null/undefined
Hello ___world__
-> 应该返回_world_
Hello ___wor_l_d___
-> 应该返回_wor_l_d_
Hello __*$world__
-> 应该返回_*$world_
Hello world__
-> 应该返回 null/undefined
现在我得到它是这样的:
const reg = /(?<=_)(.*)(?=_)/;
const string1 = "Hello ___world___";
const string2 = "Hello ______";
const string3 = "Hello ___wor_l_d___";
const string4 = "Hello ___*$world___";
const string5 = "Hello world__";
console.log(string1.match(reg));
console.log(string2.match(reg));
console.log(string3.match(reg));
console.log(string4.match(reg));
console.log(string5.match(reg));