taesik

string replace using regex 본문

functions and trick/typescript_javascript

string replace using regex

taesikk 2022. 1. 26. 10:35
function pigIt(str){
  return str.replace(/(\w)(\w*)(\s|$)/g, "\$2\$1ay\$3")
}
function pigIt(str) {
  return str.replace(/\w+/g, (w) => {
    return w.slice(1) + w[0] + 'ay';
  });
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

 

String.prototype.replace() - JavaScript | MDN

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. If pattern is a string, onl

developer.mozilla.org

 

$ can be used in replacer function on .replace() 

'functions and trick > typescript_javascript' 카테고리의 다른 글

temp  (0) 2022.01.26
is valid phone number (regex)  (0) 2022.01.26
isValidIP  (0) 2022.01.26
makes this string uppercasegives it sorted in alphabetical order by last name.  (0) 2022.01.25
Sum of numbers from 0 to N  (0) 2022.01.25