phuocng / this-vs-that

Understanding the difference between ˍˍˍ and ˍˍˍ in front-end development
https://phuoc.ng/collection/this-vs-that
MIT License
1.47k stars 99 forks source link

Update encode-uri-vs-encode-uri-component.md #223

Closed ZhaoTim closed 2 years ago

ZhaoTim commented 2 years ago

repeated \

const encode = (str) =>
        encodeURIComponent(str)
            .replace(/\\-/g, '%2D')
            .replace(/\\_/g, '%5F')
            .replace(/\\./g, '%2E')
            .replace(/\\!/g, '%21')
            .replace(/\\~/g, '%7E')
            .replace(/\\*/g, '%2A')
            .replace(/\\'/g, '%27')
            .replace(/\\(/g, '%28')
            .replace(/\\)/g, '%29');
console.log(encode('-');
image

After fix:

const encode = (str) =>
        encodeURIComponent(str)
            .replace(/\-/g, '%2D')
            .replace(/\_/g, '%5F')
            .replace(/\./g, '%2E')
            .replace(/\!/g, '%21')
            .replace(/\~/g, '%7E')
            .replace(/\*/g, '%2A')
            .replace(/\'/g, '%27')
            .replace(/\(/g, '%28')
            .replace(/\)/g, '%29');
console.log(encode('-'));
image
phuocng commented 2 years ago

Thank you, @ZhaoTim 👍