showing results for - "switching words in a string using replace"
Cristóbal
19 May 2018
1let re = /(\w+)\s(\w+)/;
2let str = 'John Smith';
3let newstr = str.replace(re, '$2, $1');
4console.log(newstr);  // Smith, John
5
similar questions
replace examples