gpt4 book ai didi

javascript - 使用正则表达式验证自定义表达式

转载 作者:行者123 更新时间:2023-11-30 15:38:05 24 4
gpt4 key购买 nike

如果我有这样的字符串

${employee} and ${} should be validate

我想获取所有包含类似

模式的子字符串

${}

并验证以 ${ 和 } 开头的字符串是否必须具有值?

假设下面的字符串

  ${employee} and ${} should be validate

它应该返回两个元素的数组

[ ${员工}, ${} ]

并且在验证时它应该将第二个元素显示为无效(因为它是空白的)

为此,我尝试了以下代码

function fetchMatches(theString, theRegex){
return theString.match(theRegex).map(function(el) {
var index = theString.indexOf(el);
return [index, index + el.length - 1];
});
}
fetchMatches(" ${employee} and ${} should be validate",/(\$)(\{)(.*)(\})/i);

但它没有返回所需的输出。

各位大侠,请多多指教

最佳答案

您可以尝试更改正则表达式

/(\$)(\{)(.*)(\})/

/(\$)(\{)[^\}]+(\})/

关于javascript - 使用正则表达式验证自定义表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41220988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com