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))
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'));
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'
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]