matching regular expression for normalization

Viewed 173

really bad with regular expression, so seeking help here the function i m writing is to accept an input, and return the normalized output so input must match certain, instead of listing all possible combination, is there someways to use RegExp?

e.g. jackjill | jack jill | jack-jill | jack&jill | jack & jill | jack-&-jill | jack and jill | jackandjill | jack-and-jill | jack n jill |..., the function will return 'Jack & Jill'

so input.match(/jack[ -&n]*jill/) covers the space, -, n, but how about the 'and'?

3 Answers

You can extend the pattern using optional character classes and an alternation using |

\bjack(?:[ &-]+|[ -]?(?:and|n)[ -]?)?jill\b

Regex demo

If what is before and after the n or and should be the same, you can use a capturing group for the part before and use a backreference to match the same content after it.

\bjack(?:[ &-]+|([ -]?)(?:and|n)\1)?jill\b

Explanation

  • \bjack Word boundary, then match jack
  • (?: Non capture group
    • [ &-]+ Match either a space, & or -
    • | Or
    • ([ -]?) Optionally match a space or - in capture group 1
    • (?:and|n) Match either and or n
    • \1 Backreference to what is captured in group 1
  • )? Close group and make ik optional
  • jill\b Match jill followed by a word boundary

Regex demo

You could replace jack[ -&n]*jill by \bjack.*?jill\b.

Details:

  • .*?: any letter between jack and jill
  • \b: word boundary

Demo

Use

replace(/\b(jack)[\s&-]*(?:(?:n|and)[\s&-]*)?(jill)\b/gi, '$1 & $2')

See proof.

*Explanation

--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    jack                     'jack'
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  [\s&-]*                  any character of: whitespace (\n, \r, \t,
                           \f, and " "), '&', '-' (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    (?:                      group, but do not capture:
--------------------------------------------------------------------------------
      n                        'n'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      and                      'and'
--------------------------------------------------------------------------------
    )                        end of grouping
--------------------------------------------------------------------------------
    [\s&-]*                  any character of: whitespace (\n, \r,
                             \t, \f, and " "), '&', '-' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    jill                     'jill'
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char

JavaScript:

const text = " jackjill | jack jill | jack-jill | jack&jill | jack & jill | jack-&-jill | jack and jill | jackandjill | jack-and-jill | jack n jill jack knows Jill";
console.log(text.replace(/\b(jack)[\s&-]*(?:(?:n|and)[\s&-]*)?(jill)\b/gi, '$1 & $2'));

Related