-1

I have a text where I need to extract the community name The delimiter for spliting is " - "

Hence Iowa - Cedar Rapids - Meth-Wick Community must give result as Meth-Wick Community

if input is Iowa - Cedar Rapids - The Gardens of Cedar Rapids result must be The Gardens of Cedar Rapids

I have tried with

"Iowa - Cedar Rapids - The Gardens of Cedar Rapids".match(new RegExp(/([A-Z])\w{3}/gi))
Code Guy
  • 3,059
  • 2
  • 30
  • 74

4 Answers4

1

Try this one out for size:

console.log("Iowa - Cedar Rapids - The Gardens of Cedar Rapids".replace(/([\w\s]+)\s-\s([\w\s]+)\s-\s([\w\s]+)/i,'$3'));
codehead
  • 11
  • 6
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 11 '21 at 06:50
1

You can do a negative lookahead to find the last occurrence of the delimiter as mentioned here https://stackoverflow.com/a/8374980/5417843 and then extract the community name from it.

input = "Iowa - Cedar Rapids - The Gardens of Cedar Rapids"
communityName = input.match(new RegExp('- (?:.(?!- ))+$')) // '- The Gardens of Cedar Rapids'
communityName[0].substring(2) // 'The Gardens of Cedar Rapids'
shri_world
  • 36
  • 5
0

Here's a simple solution, used less regex as much as possible:

"Iowa - Cedar Rapids - Meth-Wick Community".split(/ - /g).slice(-1)[0]
"Iowa - Cedar Rapids - The Gardens of Cedar Rapids".split(/ - /g).slice(-1)[0]
PauAI
  • 384
  • 4
  • 16
0

Use

const string = "Iowa - Cedar Rapids - The Gardens of Cedar Rapids"
console.log((string.match(/.*\s+-\s+(.+)/) || [''])[1])

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  -                        '-'
--------------------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    .+                       any character except \n (1 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37