-1

I'm trying to replace all - with ' ' so that I will have

laos                           //  laos
new-caledonia                  //  new caledonia
saint-lucia                    //  saint lucia
st.-vincent-grenadines         //  st. vincent grenadines 
curaçao                        //  curaçao
saint-kitts-and-nevis // saint Kitts and nevis falkland-islands // falkland islands

this RegEx match all but difficult to group-out the '-' My expression @ Regex 101

Wasiu
  • 319
  • 4
  • 13

2 Answers2

0

As @tomerpacific mentioned in the comments, if you just want to replace dash with space then you can use replace on a string like this:

str.replace(/-/g, ' ')

Regex 101

words = ['laos', //  laos
  'new-caledonia', //  new caledonia
  'saint-lucia', //  saint lucia
  'st.-vincent-grenadines', //  st. vincent grenadines 
  'curaçao', //  curaçao
  'saint-kitts-and-nevis', //  saint Kitts and nevis
  'falkland-islands' //  falkland islands
]   

words.forEach((word) => {
  console.log(word.replace(/-/g, ' '));
});
Swetank Poddar
  • 1,257
  • 9
  • 23
0

You can try this Regular Expression: /(\b[A-Za-zÀ-ÖØ-öø-ÿ.]+)/g but performance-wise string parsing would be nicer. When is it best to use Regular Expressions over basic string splitting / substring'ing?

Abhishek Duppati
  • 610
  • 6
  • 18
  • 1
    You have replaced hyphen with space and that's a great and easy task, and your answer might have solved the question. I'm just trying to help if he want to use Regex and on what case, this regex would be grateful and useful if he wants to go for it in future tasks. – Abhishek Duppati May 18 '20 at 21:59
  • Ah okay. Also, you need to add the global flag. Your regex won't work otherwise :) – Swetank Poddar May 18 '20 at 22:12
  • Yes! you are right, but / and also /g are available in regex101.com as he was trying to match on this website I've just included as it is so that there might not be any confusion. Thank you I'll update it. – Abhishek Duppati May 18 '20 at 22:16