简体   繁体   English

RegExpr 检查字符串是否仅包含 % 或仅包含空格或组合

[英]RegExpr check if a string contains only % or only whitespaces or a combination

I am trying to create a regExpr te define if a string contains a % or a whitespace or a combination of the 2.我正在尝试创建一个 regExpr te 定义字符串是否包含 % 或空格或 2 的组合。

Example:例子:

'%%%%' ---> true
'%    %%%' ---> true
'test%' ---> false

can someone help?有人可以帮忙吗?

thx a lot多谢

You can use this regex,你可以使用这个正则表达式,

^[% ]+$

Check this JS code demo,检查这个JS代码演示,

 var arr = ['%%%%','% %%%','test%'] for(s of arr) { console.log(s + ' ---> ' +/^[% ]+$/.test(s)); }

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

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