Jump to content

Adding dashes for numbers and alphabet data


rpaterick

Recommended Posts

adding dashes after every 4th character.

 

This didn't work for data below.:o

 

return Str(Field("MY_BARCODE").replace(/(\d{4})(?!$)/g,"$&-"));

SAMPLE DATA:

AXURDNNWUN2YN59H

BGV9RFT3WQAD3K7J

6JYDM2CYXCKAZQJ6

KYEW4ZS6G9F4A449

UK2QWYPE5SVT58LY

R84QSLF8TT5C6YFM

ZT4PR8RGDVE6QLHU

93BDX97RTY467GY9

QTM6KGMVVN64VP7G

WLZTPR7SFLGCKTX6

Link to comment
Share on other sites

adding dashes after every 4th character.

 

This didn't work for data below.:o

 

return Str(Field("MY_BARCODE").replace(/(\d{4})(?!$)/g,"$&-"));

SAMPLE DATA:

AXURDNNWUN2YN59H

BGV9RFT3WQAD3K7J

6JYDM2CYXCKAZQJ6

KYEW4ZS6G9F4A449

UK2QWYPE5SVT58LY

R84QSLF8TT5C6YFM

ZT4PR8RGDVE6QLHU

93BDX97RTY467GY9

QTM6KGMVVN64VP7G

WLZTPR7SFLGCKTX6

The problem is the \d in your regular expression. That matches only digit characters (0-9), not all alphanumeric characters as in your sample data. Try \w (any alphanumeric character) instead:

return Field("MY_BARCODE").replace(/(\w{4})(?!$)/g,"$&-");

Note that the "Str" function wrapping everything is superfluous, so I removed it.

 

Also, if you want to avoid a trailing dash at the end, instead of using ?!$ to say, "not at the end of the string," just use String.match and String.join instead, like so:

return Field("MY_BARCODE").match(/(\w{4})/g).join("-");

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...