-2

Here I am a using regular expression to remove the html input tag. But I have to replace with its value. I need to remove the input tag without the value. Result should be <td> 15200 </td> on td tag.

function fnTextValue() {
  var tab_text = "<td><input name='MyTextBox' type='text' value='15200' id='MyTextBoxId'></td>";
  console.log(tab_text);
  
  tab_text = tab_text.replace(/<input[^>]*>|<\/input>/gi, "");
  console.log(tab_text);
}
<button id="btnExport" onclick="fnTextValue();">
  Text Value
</button>
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
nazmul.3026
  • 918
  • 1
  • 9
  • 20
  • 3
    why use a regex? could you not use `getElementsByTagName` then loop through them and use `replaceWith` or something similar whilst getting their value in the loop? – Pete Nov 11 '19 at 09:47
  • Actually my real table is so large that's why i use this regex. – nazmul.3026 Nov 11 '19 at 09:48

1 Answers1

1

String operation faster than RegEx you can go with @Pete idea. If you really try to solve with regex you can do as follows.

tab_text = tab_text.replace(/<input[^>]*value\s*=\s*["'](.+?)["']\s*[^>]*>/,"$1");

And the JS solution is

var ele = document.getElementsByTagName("input");
for(var x of ele){ 
    if(x){
    var val = x.value;
    var node = document.createTextNode(val); 
    x.after(node);
    x.remove();
}
mkHun
  • 5,891
  • 8
  • 38
  • 85