1

I have a return data string from an Oracle process that outputs [TD] and [/TD] rather than <TD> and </TD> to denote an html table cell. I need to translate [TD] to <TD> and [/TD] to <TD> in an element with an ID of unchangedValues using Javascript.

I've Googled and searched on here and this is the closest I've got so far, neither of which work. Any help here would be most gratefully received.

function strReplace() {
    var string = document.getElementById('unchangedValues').innerHTML;
    var res1 = string.replace(/(\[)|(\])/g, function(match, p1, p2) {
  if (p1) return "<";
  if (p2) return ">"
});
}

and I've also tried:

function substituteValues() {

  const str = document.getElementById('unchangedValues').innerHTML;
  const res1 = str.replace(/\[TD\]/g, '\<TD\>');

  document.getElementById('unchangedValues').innerHTML = res1;
}

Thanks

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
enCue
  • 107
  • 1
  • 8

5 Answers5

2

You may use

console.log(
   "[TD] and [/TD]".replace(/\[(\/?TD)]/gi, '<$1>')
)

The /\[(\/?TD)]/gi regex matches

  • \[ - a [
  • (\/?TD) - Group 1: / 1 or 0 times and then TD
  • ] - a ] char
  • /gi - multiple occurrences, case insensitive.

If there may be attributes in TD use

// Assuming values can't contain ] and " are value qualifiers
console.log( '[TD key="value"] and [/TD]'.replace(/\[(\/?TD(?:\s[^\]]*)?)]/gi, '<$1>') );
// Assuming values can contain ] and " or ' are value qualifiers
console.log( '[TD key="va[l]ue here" key2=\'value 2\' auto ] and [/TD]'.replace(/\[(\/?TD(?:\s+\w+(?:=(?:"[^"]*"|'[^']*'|\w+))?)*)\s*]/gi, '<$1>') );
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

Generic solution for any tag, not just [TD]:

console.log(
  'Lorem [TD FOO="ipsum"] dolor sit [/TD] amet'
  .replace(/\[([^\]]+)\]/g, '<$1>')
);
P Varga
  • 19,174
  • 12
  • 70
  • 108
0

How about that with 2 str.replace() only for brackets?

var str = 'John [TD] Smith [/TD]';
var newstr = str.replace(/\[/gi, '<').replace(/]/gi,'>');
console.log(newstr);
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0

If your id is on a table element or even a tr element, and the format is not correct (I don't know since you didn't post the HTML), the browser will move the invalid stuff outside of the table (or closest valid location). You can see that when run, there is no data within the tr and this is making your code attempt to format an empty string then save the empty string back.

console.log(document.getElementById('unchangedValues').innerHTML)
<table id="unchangedValues">
  <tr>
    [TD]Hi[/TD]
    [TD]Hi[/TD]
    [TD]Hi[/TD]
    [TD]Hi[/TD]
  </tr>
</table>

As you can see by the output, the [TD] and [/TD]'s are all gone from the table, however if you inspect the output, the values will have been moved above or below the table automatically by the browser.

To fix the issue, you need have the items within memory. You can then modify the items and then save them into the table.

let str = `[TD]Hi[/TD] 
    [TD]Hi[/TD] 
    [TD]Hi[/TD] 
    [TD]Hi[/TD]`
    
document.querySelector('#unchangedValues tr').innerHTML = str.replace(/\[(\/?TD)\]/g, '<$1>')
<table id="unchangedValues">
  <tr>
  </tr>
</table>
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
0

you can also check an replace with the regExp of Wiktor Stribiżew

if(str.indexOf('[TD]')!==-1 || str.indexOf('[/TD]')!==-1){str = str.replace(/\[(\/?TD)]/gi, '<$1>')}