I am using react building an ecommerce. I am getting data from API and setting is as dangerouslySetInnerHTML into JSX. I need to remove some text/word from the API data. I added a picture with mark.
How do I remove that?
with(document)with(body)with(insertBefore(createElement("script"),firstChild))setAttribute("exparams","userid=&aplus&ali_beacon_id=&ali_apache_id=&ali_apache_track=&ali_apache_tracktmp=&dmtrack_c={}&hn=aeproductsourcesite033001217071%2eus44&asid=AQAAAADkyhRi8NcdZwAAAABHROT4NDi+yA==&sidx=Fzc+BeTKFGIAAAAA0uvd1jNUkicntMPM",id="beacon-aplus",src="//assets.alicdn.com/g/alilog/??aplus_plugin_aefront/index.js,mlog/aplus_v2.js")
Package included:1 Dress
Material:100% Cotton
Colors:Beige,Navy,Light Green,Pink
Size:S,M,L,XL,2XL,,,
Sleeve Length:Full Sleeve
Neckline:Square Neck
Pattern: Solid Color
Length:Ankle Length
Thickness:Thin
Decoration:Side Pockets
Style:Leisure,Bohemian,Retro,Europe
Season:Spring,Summer,Autumn
Occasion:Family,Holiday,Employment,Travel
I need to remove first part of text:
Picture: https://prnt.sc/RNl4TG6habCe
One options is that you can use regex with either the split()
method, or using the match()
method instead. The regular expression will vary based on the method you choose to use.
const data = `with(document)with(body)with(insertBefore(createElement("script"),firstChild))setAttribute("exparams","userid=&aplus&ali_beacon_id=&ali_apache_id=&ali_apache_track=&ali_apache_tracktmp=&dmtrack_c={}&hn=aeproductsourcesite033001217071%2eus44&asid=AQAAAADkyhRi8NcdZwAAAABHROT4NDi+yA==&sidx=Fzc+BeTKFGIAAAAA0uvd1jNUkicntMPM",id="beacon-aplus",src="//assets.alicdn.com/g/alilog/??aplus_plugin_aefront/index.js,mlog/aplus_v2.js")
Package included:1 Dress
Material:100% Cotton
Colors:Beige,Navy,Light Green,Pink
Size:S,M,L,XL,2XL,,,
Sleeve Length:Full Sleeve
Neckline:Square Neck
Pattern: Solid Color
Length:Ankle Length
Thickness:Thin
Decoration:Side Pockets
Style:Leisure,Bohemian,Retro,Europe
Season:Spring,Summer,Autumn
Occasion:Family,Holiday,Employment,Travel`;
const removeWithSplit = (data) => data.split(/(\n|\r){2,}/).pop();
const removeWithMatch = (data) => data.match(/(\w\s?)+:.*/g)?.join('\n');
console.log('**WITH SPLIT**');
console.log(removeWithSplit(data), '\n');
console.log('**WITH MATCH**');
console.log(removeWithMatch(data), '\n');
Bonus:
It's a bummer the API returns this plain text. Is it possible to get JSON back instead? If not, here's a way to convert its results into an object:
const data = `with(document)with(body)with(insertBefore(createElement("script"),firstChild))setAttribute("exparams","userid=&aplus&ali_beacon_id=&ali_apache_id=&ali_apache_track=&ali_apache_tracktmp=&dmtrack_c={}&hn=aeproductsourcesite033001217071%2eus44&asid=AQAAAADkyhRi8NcdZwAAAABHROT4NDi+yA==&sidx=Fzc+BeTKFGIAAAAA0uvd1jNUkicntMPM",id="beacon-aplus",src="//assets.alicdn.com/g/alilog/??aplus_plugin_aefront/index.js,mlog/aplus_v2.js")
Package included:1 Dress
Material:100% Cotton
Colors:Beige,Navy,Light Green,Pink
Size:S,M,L,XL,2XL,,,
Sleeve Length:Full Sleeve
Neckline:Square Neck
Pattern: Solid Color
Length:Ankle Length
Thickness:Thin
Decoration:Side Pockets
Style:Leisure,Bohemian,Retro,Europe
Season:Spring,Summer,Autumn
Occasion:Family,Holiday,Employment,Travel`;
const removeWithMatch = (data: string) => data.match(/(\w\s?)+:.*/g)?.join('\n');
const resultsToObject = (data: string) => {
return data.split('\n').reduce((acc, curr) => {
const [key, value] = curr.split(':');
return {
...acc,
[key]: value,
};
}, {} as Record<string, unknown>);
};
console.log(resultsToObject(removeWithMatch(data) as string));