简体   繁体   English

正则表达式-匹配parsley.js中的整个单词

[英]Regex - match whole word in parsley.js

How do I match two entire words with or(|) operand in parsley.js' data-parsley-pattern attribute? 如何在parsley.js的data-parsley-pattern属性中使用or(|)操作数匹配两个完整的单词?

As you can see I want to match the words user and moderator . 如您所见,我要匹配usermoderator

I have tried: data-parsley-pattern="/(user|moderator)/" which is normal regex and works with other standardised regular expression matching like regex101.com: https://regex101.com/r/kD7sR1/1 我曾尝试: data-parsley-pattern="/(user|moderator)/"这是正常的正则表达式,并与其他标准化的正则表达式匹配等regex101.com作品: https://regex101.com/r/kD7sR1/1

See the data-parsley-pattern 2.0 documentation : 请参阅data-parsley-pattern 2.0文档

Validates that a value matches a specific regular expression (regex). 验证值是否与特定的正则表达式(regex)匹配。 Note that patterns are anchored, ie must match the whole string. 请注意,模式是固定的,即必须匹配整个字符串。 Parsley deviates from the standard for patterns looking like /pattern/{flag} ; 对于类似于/pattern/{flag}模式,Parsley偏离了标准; these are interpreted as litteral regexp and are not anchored. 这些被解释为立面正则表达式,并且不固定。

So, it looks like your regex should work. 因此,看起来您的正则表达式应该正常工作。

If it does not, try 如果没有,请尝试

data-parsley-pattern="/[\s\S]*(user|moderator)[\s\S]*/"

The [\\s\\S]* will match zero or more characters (any, including newlines). [\\s\\S]*将匹配零个或多个字符(任何字符,包括换行符)。

Your pattern should work, as long as you are using a full form with /.../ , otherwise the pattern would be anchored. 只要您使用带有/.../的完整格式,您的模式就应该起作用,否则该模式将被锚定。 You don't even need the parenthesis but they won't hurt either. 您甚至不需要括号,但它们也不会受到伤害。

Here's a working demo that simply uses 这是一个可以正常使用的演示

<input data-parsley-pattern="/user|moderator/" type="text" name="fullname" required>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM