简体   繁体   中英

Replace ascii codes with strings in Javascript

I created a simple javascript function that receives a string and parses through it and updates other form fields. The function is called when I use a scanner to read a 2D barcode. My problem is that there are multiple fields separated with ascii code 29 which is the "group separator". I'm trying convert all the special characters to a readable string like "". I tried doing a replace like:

var BC = barcodestring.replace(String.fromCharCode(29), '');

Also, will the "replace" function replace just 1 or all of the occurrences?

This should work to replace the default replace function to it replaces all of that character.

barcodestring  = barcodestring.replace(new RegExp(String.fromCharCode(29).replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), '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