简体   繁体   中英

Javascript regexp not replacing/matching

I want to remove the regex content from my string but it works on the regex site ( https://regex101.com/r/XSpa6d/2 ) but not with my code. What did I do wrong cuz cant seem to find it? ( https://jsfiddle.net/eL8r4b1j/ )

 const string = "random characterhundsguhiosdg window.__AH_DATA__= {gert}\n\n window.__APOLLO_STATE__= {} random characterhundsguhiosdg" const r = new RegExp(/window\.__AH_DATA__= {.*}\\n\\n window\.__APOLLO_STATE__= {}/gm) const replaced = string.replace(r, "") console.log(replaced)

New lines should not be double escaped.

 const string = "random characterhundsguhiosdg window.__AH_DATA__= {gert}\n\n window.__APOLLO_STATE__= {} random characterhundsguhiosdg" const r = new RegExp(/window\.__AH_DATA__= {.*}\n\n window\.__APOLLO_STATE__= {}/gm) const replaced = string.replace(r, "") console.log(replaced)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM