简体   繁体   中英

How to remove ampersand, pound & number from a http response string in Javascript

I currently have an API that returns the following string inside parentheses

(Multivitamin Gummies - Berry, Peach & Orange - 150ct - Up&Up™)

I want it to be instead formatted to the user like so

(Multivitamin Gummies - Berry, Peach & Orange - 150ct - Up&Up)

Is there a way to dynamically do this fix this in javascript?

Here is another example:

(Aloe Hand Sanitizer Gel - 8 fl oz - Up&Up™) 

should return

(Aloe Hand Sanitizer Gel - 8 fl oz - Up&Up)

I hope this helps

 var str = 'Aloe Hand Sanitizer Gel - 8 fl oz - Up&Up™'; console.log(str.match(/[^™]+/)[0]);

Add the characters you want to replace in the or |like below in the regex and replace using .replace()

/™|&#38|&#8482/g

 var str = 'Aloe Hand Sanitizer Gel - &#8482 8 fl oz - Up&Up™'; console.log(str.replace(/™|&#38|&#8482/g, ""));

Looks like you want to replace all Unicode with empty string.

You can use regex str.replace(/(&#\d+;)/g,"") to remove these characters

 var str = '(Multivitamin Gummies - Berry, Peach & Orange - 150ct - Up&Up™)'; console.log(str.replace(/(&#\d+;)/g,"")); str = '(Aloe Hand Sanitizer Gel - 8 fl oz - Up&Up™) '; console.log(str.replace(/(&#\d+;)/g,""));

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