000
000

Reputation: 230

Replace not working [JS]

Just a quick question in javascript.

In the following line

let str = "This sentence is great (amazing even) [yeah, whatever, screw grammar]"
let newStr = str.replace(/"\["|"\]"|"\("|"\)"|","/," ");

When I console.log(str), it gives me the expected string, but when I console.log(newStr), it looks like the .replace didn't do anything... it simply returns str.

newStr should be "This sentence is great amazing even yeah whatever screw grammar"

Can anyone resolve this issue?

Upvotes: 0

Views: 217

Answers (1)

0xdw
0xdw

Reputation: 3842

Your regex pattern is wrong. Below is the correct one,

let str = "This sentence is great (amazing even) [yeah, whatever, screw grammar]"
let newStr = str.replace(/\(|\)|\[|\]|,/g,"");
console.log(newStr)

Upvotes: 1

Related Questions