String Method

Method Table

Match(regex)

// 1
"hannah1".match(/\d/i)
// 2
let regex1 = new RegExp(/\d/, "i")
"hannah1".match(regex1)

Replace(egexp|substr, newSubstr|function)

原來 replace 也是可以帶 function 的喔

function 參數解釋

function replacer(match, p1, p2, p3, offset, string) {
  // p1 is nondigits, p2 digits, and p3 non-alphanumerics
  return [p1, p2, p3].join(' - ');
}
var newString = 'abc12345#$*%'.replace(/([^\d]*)(\d*)([^\w]*)/, replacer);
console.log(newString);  // abc - 12345 - #$*%

Last updated