data.replace to format all '/' to '_'.

data.replace to format all '/' to '_'.

BalaKrishnanDhanuskodiBalaKrishnanDhanuskodi Posts: 45Questions: 17Answers: 0
edited November 2017 in DataTables 1.10

Happy Evening one and All,

I trying to replace all the slash '/' to '_' of the invoice file data. following is the observation.

data = 'INV/001/002/AXS001'
desiered out = INV_001_002_AXS001

{
targets : 2,
render: function (data, type, row ) {
data_replace = data.replace('/', '_');
return '<a href="invoice/'+data_replace+'.pdf">' + data + '</a>';
},

Option 1 - resulted 'INV_001/002/AXS001' - is not right format

Option -2 - currently using this to get the desired out and I am sure this is not the right code.

{
targets : 2,
render: function (data, type, row ) {
 data_replace = data.replace('/', '_');
 data_replace1 = data_replace.replace('/', '_');
 data_replace2 = data_replace1.replace('/', '_');
 data_replace3 = data_replace2.replace('/', '_');
return '<a href="invoice/'+data_replace3+'.pdf">' + data + '</a>';
},

Any help would be grateful.

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    The js "replace" function requires a "global" parameter if it is to replace more than one instance.

  • BalaKrishnanDhanuskodiBalaKrishnanDhanuskodi Posts: 45Questions: 17Answers: 0

    Happy Evening Tangerine,

    Following option is not working

    data_replace = data.replace('/\//g', '_');

  • allanallan Posts: 63,691Questions: 1Answers: 10,500 Site admin
    Answer ✓

    Drop the single quotes around the regex to make it a regex object:

    data_replace = data.replace(/\//g, '_');
    

    Allan

  • BalaKrishnanDhanuskodiBalaKrishnanDhanuskodi Posts: 45Questions: 17Answers: 0

    Thanks Allan,

  • BalaKrishnanDhanuskodiBalaKrishnanDhanuskodi Posts: 45Questions: 17Answers: 0

    Hi Allan, the table stuck saying ' Loading...'
    :/ :/

  • kthorngrenkthorngren Posts: 21,450Questions: 26Answers: 4,975

    Does your browser's console show any errors?

    Kevin

This discussion has been closed.