0

I want to create a regular expression for a string, if that string exit then it should return 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]+)$/

Shiv Singh
  • 6,939
  • 3
  • 40
  • 50
  • I think there might be few typos (exit -> exists?), so please fix them to make your problem clearer to others... also please include relevant test cases and expected output... – Hitmands May 22 '19 at 11:22
  • If you want to match it when it is not present you could use `^(?!.*Please select)[a-zA-Z0-9 ]+$`. Perhaps you could also use a form of contains if you are using code. – The fourth bird May 22 '19 at 11:23
  • If `/^(?!.*Please select)([a-zA-Z0-9]+)$/` works for you why use the redundant lookahead? Use `/^[a-zA-Z0-9]+$/`. Duplicate of [RegEx for Javascript to allow only alphanumeric](https://stackoverflow.com/questions/388996). – Wiktor Stribiżew May 22 '19 at 11:48

3 Answers3

0

This regex should do:

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

It will select all line that does not contain Please select anywhere in the line.
This will work with any character on the line, nut just letter and numbers.

Jotne
  • 40,548
  • 12
  • 51
  • 55
  • This is working : /^(?!.*Please select)([a-zA-Z0-9]+)$/ – Shiv Singh May 22 '19 at 11:33
  • @ShivSingh No, try to add a `.` or `%` other symbol to the line with your regex. – Jotne May 22 '19 at 11:39
  • @Jotne Not my downvote, I believe OP just wants to match an alphanumeric string, and the lookahead is just redundant in their pattern. I'd re-close with [RegEx for Javascript to allow only alphanumeric](https://stackoverflow.com/questions/388996). Please consider casting a vote since my dupe-hammer was undone. – Wiktor Stribiżew May 22 '19 at 11:51
0

If you are using 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;
Dudi Boy
  • 4,551
  • 1
  • 15
  • 30
0

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
chatnoir
  • 2,185
  • 1
  • 15
  • 17