0

I am using a regex pattern to find a unique name. How can I make my negative lookahead patten (?!...) ignoring upper or lowercase: I already tried (?i) but it makes the whole pattern ignore the very negative lookahead!!!!

here is my pattern:

pattern = "(^(?!(david|hello|dreamer|John_Doe|socceroos)$).*)(^[\w].*)"

But it accepts the same word with different letter case! for example if I enter DaVid its still a valid entry!

and I want it to ignore case sensitivity if there is a match! And how can I invalid double-space anywhere in my string too?

any ideas my friends?

  • Thanks for your informative response. In that example I want the pattern to find a word no matter if upper or lowercase. for example it has to be able to find and fail no matter if it is "david", "David", DAViD", "dAviD" or so on... What I mean is the negative lookahead ignores if the character is upper or lowercase! and does not pass the pattern. –  Sep 11 '22 at 13:29
  • sorry for giving you a vague explanation. Here is what I want in return: https://regex101.com/r/0cIlYB/2 –  Sep 11 '22 at 14:50
  • https://regex101.com/r/0cIlYB/3 is this what you are looking for? `^(cat|dog)$` – Claudio Sep 11 '22 at 16:01
  • yes, that's an example though! anyhow, I just found out that there's no way to make an _html pattern_ ignoreCase!!! as (?i) is not supported in JavaScript and html does in favor of JS regex only! :( –  Sep 11 '22 at 16:08
  • https://regex101.com/r/0cIlYB/4 this one does it too ... `^(?=(cat|dog)$)` – Claudio Sep 11 '22 at 16:14
  • 1
    Ignore case is a flag you set along the regex pattern as optional parameter – Claudio Sep 11 '22 at 16:16
  • 1
    For javascript the Code Generator at regex101 says: `const regex = /^(?=(cat|dog)$)/gmi;` for setting insensitive search. Or `const regex = new RegExp('^(?=(cat|dog)$)', 'gmi')` – Claudio Sep 11 '22 at 16:20

1 Answers1

0

You can omit the outer capture group around the whole pattern, and grouping the alternatives can be a non capture group (?: instead.

The pattern can be written as:

^(?!(?:david|hello|dreamer|John_Doe|socceroos)$|.*  )\w.*

Explanation

  • ^ Start of string
  • (?! Negative lookahead
    • (?:david|hello|dreamer|John_Doe|socceroos)$ Match any of the names till the end of the string.
    • | Or
    • .* Match 2 spaces (Or .*\s\s but note that \s can also match a newline)
  • ) Close the lookahead
  • \w.* Match a single word char and the rest of the line

Regex demo

In JavaScript, if you want to find multiple lines, you can use /gmi; as the flags for global, multiline and case insensitive.

const regex = /^(?!(?:david|hello|dreamer|John_Doe|socceroos)$|.*  )\w.*/i;
[
  "david",
  "DaVid",
  "this is a      test.",
  "this is a test.",
  "David"
].forEach(s =>
  console.log(regex.test(s) ? `Match for "${s}"` : `No match for "${s}"`));
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • Thanks for the double space part, it's working very well. but the negative lookahead on **pattern** is still case-sensitive. For example it passes David. which I expect it not to! and _i flag_ is not acceptable in JS! :( –  Sep 11 '22 at 17:26
  • 1
    @MahmoudMirzaei It does not pass for `David` see https://regex101.com/r/sleZ6Y/1 – The fourth bird Sep 11 '22 at 17:26
  • yes because regex option is set on `gmi`: `/^(?!(?:david|hello|dreamer|John_Doe|socceroos)$|.* )\w.*/gmi` try this https://regex101.com/r/sleZ6Y/2 –  Sep 11 '22 at 17:29
  • 1
    @MahmoudMirzaei I have added example code. Javascript supports `/i` and if you use the [RegExp constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp) the `i` should be in the second parameter like `const regex = new RegExp('^(?!(?:david|hello|dreamer|John_Doe|socceroos)$|.* )\\w.*', 'gmi') ` – The fourth bird Sep 11 '22 at 17:34
  • Sure thanks. Is there any way to apply the new regex for pattern side? or I must forget the pattern to exploit ignoreCase option? –  Sep 11 '22 at 17:38
  • @MahmoudMirzaei I am not sure what you mean by that, can you give an example using a regex101 example link? – The fourth bird Sep 11 '22 at 17:39
  • what I am after is here: https://stackoverflow.com/questions/5524842/have-html5s-a-inputs-pattern-attribute-ignore-case –  Sep 11 '22 at 17:44
  • To get this straight, to get bootstrap validation UI, I need to get an invalid html built-in invalidity. which will be resulted through an invalid pattern attribute on an html text input element! –  Sep 11 '22 at 17:49
  • 1
    @MahmoudMirzaei Then you should write the names like `[Dd][Aa][Vv][Ii][Dd]` – The fourth bird Sep 11 '22 at 17:54
  • 1
    Thanks. That's a pretty good idea! Thanks the4thBird –  Sep 11 '22 at 19:12