[英]JS RegEx allow certain characters and a few character only when part of a specific sequence
For quite a few hours I have been struggling with the following:几个小时以来,我一直在努力解决以下问题:
I want to use RegEx using JS to allow the following characters:我想使用 JS 使用 RegEx 来允许以下字符:
/^[a-zA-Z0-9 -.,]*$/igm
However I would also want to allow但是我也想允许
or要么
, which means I would allow the character < & > and /, but only when in either of the two forms above ( ,这意味着我将允许字符 < & > 和 /,但仅当在上面两个 forms 中的任何一个时(
or要么
). ). I was playing with something like;
我在玩类似的东西;
/^(<(?=br \/>|br\/>))*&/img
Clearly this is not working, and I cannot seem to figure out how to test in one line of code whether the user input (a string) contains just the allowed characters plus the extra characters(<,>,/) but only when used as (显然这是行不通的,我似乎无法弄清楚如何在一行代码中测试用户输入(字符串)是否只包含允许的字符加上额外的字符(<、>、/),但仅当用作(
or要么
). ).
Your help is very much appreciated.非常感激您的帮忙。 Thank you.
谢谢你。
You can use您可以使用
^(?:<br ?\/>|[a-zA-Z0-9 .,-])*$
Details细节
^
- start of string ^
- 字符串的开始(?:<br?\/>|[a-zA-Z0-9.,-])*
- zero or more: (?:<br?\/>|[a-zA-Z0-9.,-])*
- 零个或多个:
<br?\/>
- <br
, an optional space, />
<br?\/>
- <br
,可选空格, />
|
- or [a-zA-Z0-9.,-]
- a letter, digit, space, dot, comma or hyphen [a-zA-Z0-9.,-]
- 字母、数字、空格、点、逗号或连字符$
- end of string. $
- 字符串结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.