Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Switching words in a string using replace

let re = /(w+)s(w+)/;
let str = 'John Smith';
let newstr = str.replace(re, '$2, $1');
console.log(newstr);  // Smith, John
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #Switching #words #string #replace
ADD COMMENT
Topic
Name
8+5 =