Regex - any alphabet except "e"

I want to make a regular expression for any alphabets except "e". This is what I came up with -

/([a-d]|[f-z])+?/i
  • The above regex does not match "e", which is good.
  • It corresponds to "amrica"
  • But it also corresponds to America, but it should not be due to the e in America

What am I doing wrong?

+4
source share
4 answers

You need anchors ^(beginning of line) and $(end of line); otherwise your template may partially match any string if it contains an alphabet other than e:

/^[a-df-z]+$/i.test("america")
// false

/^[a-df-z]+$/i.test("amrica")
// true

/^[a-df-z]+$/i.test("e")
// false
+4
source

[a-z], (?!e), (?:...) :

/^(?:(?!e)[a-z])+$/i

regex.

, . Java , JS RegExp ( Python re). , Java s.matches("(?i)[a-z&&[^e]]+").

:

  • ^ -
  • (?:(?!e)[a-z])+ - 1 a-z a-z BUT e e
  • $ -
  • i - .

JS demo:

var rx = /^(?:(?!e)[a-z])+$/i;
var strs = ['america','e', 'E', 'world','hello','welcome','board','know'];
for (var s of strs) {
  console.log(s, "=>", rx.test(s));
}
Hide result
+3

Capital reg, :

^[a-df-zA-DF-Z]+$

i.test("America")
// false

i.test("Amrica")
// true

i.test("e")   or  i.test("E") 
// false

, -, , https://regex101.com

+1

:

  • lookahead:

    /^((?=[a-z])[^e])+$/i
    

, , , e ( e).

regex JavaScript, , .

  1. set intersection:

    /^[[a-z]&&[^e]]+$/i
    

"a-z" "not e".

JavaScript. , , . "" , Java, Perl, PHP, Python Ruby.

  1. :

    /^[[a-z]--[e]]+$/i
    

"a-z" e.

JavaScript. , , . "" , Python .Net. ( , - vs --.)

0

Source: https://habr.com/ru/post/1680167/


All Articles