简体   繁体   English

正则表达式不包括单词

[英]Regular expression for not including a word

I want to create a regular expression for a string, if that string exit then it should return false. 我想为字符串创建一个正则表达式,如果该字符串退出则它应该返回false。

i am using it on select option, so if value is Please select then it will be false 我在选择选项上使用它,所以如果值是请选择那么它将是假的

My impression: 我的印象:

^/(?!Please select)([a-z0-9]+)$ 

Got working expression as: /^(?!.*Please select)([a-zA-Z0-9]+)$/ 得到的工作表达为:/ /^(?!.*Please select)([a-zA-Z0-9]+)$/

This regex should do: 这个正则表达式应该做:

^(?!.*Please select).+$

It will select all line that does not contain Please select anywhere in the line. 它将选择所有不包含的行Please select行中的任何位置。
This will work with any character on the line, nut just letter and numbers. 这适用于线上的任何字符,坚果只是字母和数字。

If you are using javascript. 如果你使用的是javascript。 It will be easier and more readable to find the match and just to reverse the match result. 找到匹配将更容易,更易读,只是为了反转匹配结果。 Something like: 就像是:

 let unmatched = yourLine.match(/Please select/g) == null ? true: false;

This is a simple way to validate your input against presence of "Please select": 这是一种简单的方法来验证您的输入是否存在“请选择”:

 function isValid(input) { let regex = /Please select/; return !regex.test(input); } console.log(isValid("Please select following")); //false console.log(isValid("selected answer!")); //true 

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

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