正则表达式从字符串中排除%3

时间:2022-11-07 00:11:21

I have a JavaScript function that tests a string and validates if it contains letters, numbers, and a few special characters, including the characters %20 (space) %27 (apostrophe).

我有一个JavaScript函数来测试字符串并验证它是否包含字母,数字和一些特殊字符,包括字符%20(空格)%27(撇号)。

My current RegEx fails as it allows other characters like %3

我当前的RegEx失败,因为它允许其他字符,如%3

I would like to fail on any match of %3 in the string. Better still, I would like to only match on %20 and/or %27 as a group.

我想在字符串中的%3匹配失败。更好的是,我想只在%20和/或%27上匹配。

My RegEx is

我的RegEx是

^[0-9a-zA-Z\%20\-_:.,!\/\\%27]+$

I want this to match

我想要这个匹配

Employee%27s%20Saved

But fail something like

但是失败了

%3Emplo%3yee%27s%20Saved%3

2 个解决方案

#1


0  

Update to @MohaMad comment:

更新@MohaMad评论:

^(?:[0-9a-zA-Z-_:.,!/\\]|%20|%27)+$

By using a non-capturing group it will result with full match only.

通过使用非捕获组,它将仅与完全匹配。

#2


0  

Try using a 'non-capturing group' (?!...).

尝试使用“非捕获组”(?!...)。

You should try something like this: ^(\w*(?!\%3)\w*(?:\%27|\%20)\w*)+$

你应该尝试这样的事情:^(\ w *(?!\%3)\ w *(?:\%27 | \%20)\ w *)+ $

Check https://regex101.com/r/r89dlA/1

#1


0  

Update to @MohaMad comment:

更新@MohaMad评论:

^(?:[0-9a-zA-Z-_:.,!/\\]|%20|%27)+$

By using a non-capturing group it will result with full match only.

通过使用非捕获组,它将仅与完全匹配。

#2


0  

Try using a 'non-capturing group' (?!...).

尝试使用“非捕获组”(?!...)。

You should try something like this: ^(\w*(?!\%3)\w*(?:\%27|\%20)\w*)+$

你应该尝试这样的事情:^(\ w *(?!\%3)\ w *(?:\%27 | \%20)\ w *)+ $

Check https://regex101.com/r/r89dlA/1