已关闭,此问题需要details or clarity。它目前不接受回答。
**想改善这个问题吗?**通过editing this post添加详细信息并澄清问题。
上个月关门了。
Improve this question
我正在评估字符串中包含通配符的字符串组合。具体来说,我需要同时处理“”和“?“我的正则表达式中的通配符。
例如:如果我有一个字符串PHO12RO,我喜欢在字符串中使用PHO12RO,比如PH?12RO或PH?12 例如,字符串PHO12RO将计算为true,但字符串PHO13RO将计算为false,如果我使用此模式PH?10 *
我已经设法使用正则表达式处理了字符串ex(PHO12*)中的“”字符串,但我很难匹配?“在我的字符串前(PH?12 RO)。
下面是我成功使用“”键的代码片段:
有人能指导我如何修改我的正则表达式来处理字符串场景PH吗?12* 或PH?12RO。
async function WildCardEvaluator(field:string, rule:string) : Promise<Boolean> {
// for this solution to work on any string, no matter what characters it has
var escapeRegex = (field:string) => field.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
// "." => Find a single character, except newline or line terminator
// ".*" => Matches any string that contains zero or more characters
rule = rule.split("*").map(escapeRegex).join(".*");
// "^" => Matches any string with the following at the beginning of it
// "$" => Matches any string with that in front at the end of it
rule = "^" + rule + "$"
//Create a regular expression object for matching string
var regex = new RegExp(rule);
//Returns true if it finds a match, otherwise it returns false
return regex.test(field);
}
1条答案
按热度按时间tjvv9vkg1#