Home > front end >  Converting into regular expression
Converting into regular expression

Time:12-28

I was studying about regular expressions today and having some tests.

And I'm confused about how to change these examples into regular expressions.

apple, apples, Apple, APPLE, Apples --> RE?

/[aA][pP][pP][lL][eE][ s]/

I tested in a regexr.com and it didn't work.

I would be thankful if you can tell me what I did wrong and there is a easier way to convert those words.

CodePudding user response:

Your regex matches the following:

❌ apple 
✅ apples
❌ Apple
❌ APPLE
✅ Apples

The reason it matches apples and Apples but not apple is because the last entry is [ s] which means space or lower-case s. If there's no space at the end, it won't match.

As others suggested, use the i flag to ignore case and the question mark for "zero or one" copies of the preceding character.

/apples?/i

You can also selectively ignore case:

/(?i)apples?/

Which means you can ignore case on just the first character:

/(?i)a(?-i)pples?/

That last one will match Apples and apple, but not APPLE or APPLES.

CodePudding user response:

Issues with the existing solution

  • The regular expression is not anchored to the start and end of the string. This means that it will match any occurrence of the pattern "apple" or "Apple" within the string, rather than the entire string.
  • The regular expression includes a space character ([ s]) at the end, which means that it will only match strings that end with a space. For example, it will match "apple " but not "apple" or "apples".

Use the below regex which solves your case

^[aA][pP][pP][lL][eE][sS]?$

Explanation:

  • The ^ and $ symbols match the start and end of the string, respectively, ensuring that the entire string is matched and not just a part of it.
  • The [aA] pattern will match either "a" or "A".
  • The [pP] pattern will match either "p" or "P".
  • The [lL] pattern will match either "l" or "L".
  • The [eE] pattern will match either "e" or "E".
  • The [sS]? pattern will match zero or one "s" or "S" characters.

CodePudding user response:

Your regex /[aA][pP][pP][lL][eE][ s]/ works for apples, but not apple because your regex ends in [ s] (a space or s). You could change that to [s, ] to account for commas in your input.

Here is a shorter version of the regex that also gets rid of false positives like pineapple or applepie:

const input = 'apple, apples, Apple, APPLE, Apples, pineapple, applepie';
const regex = /\bapples?\b/gi;
const matches = input.match(regex);
console.log(matches);

Output:

[
  "apple",
  "apples",
  "Apple",
  "APPLE",
  "Apples"
]

Explanation of regex:

  • \b -- word boundary to exclude pineapple
  • apple -- literal string
  • s? -- optional s
  • \b -- word boundary to exclude applepie
  • /gi -- flags: g to match multiple times, i to ignore case

Learn more about regex: https://twiki.org/cgi-bin/view/Codev/TWikiPresentation2018x10x14Regex

  • Related