DEV Community

Ruxin Qu
Ruxin Qu

Posted on • Edited on

JavaScript: String Method

  1. .search() .match() .replace() with RegExp
  • RegExp is a sequence of characters that form a search pattern. Syntax: /pattern/modifiers;.
  • Modifiers:

i(perform case-insensitive match). Return the index of the first match.

.match(/char/g) performs global match, doesn't stop after find the first matched. It will return an array containing all the matching result.

m(perform multiline matching)

  • .search() and .replace() are case-sensitive, adding RegExp can make it case-insensitive.

source referenced: W3school


2.

const first = string.indexOf(searchString)
const second = string.indexOf(searchString, (first +1));

//starts from the position after the first index
Enter fullscreen mode Exit fullscreen mode

3..split()

const str = 'ssss=dddd';
const back = str.split('=')[1];
console.log(back). //output: 'dddd' 
console.log(str.split('')) //output: [
  's', 's', 's',
  's', '=', 'd',
  'd', 'd', 'd'
]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)