There are several forms on the page, where the text marked up with html-tags is inserted, but previously passed through google or Yandex translator. Translators break some tags and instead of the original <span>fish text</span> they can produce < span> рыбный текст< / span> .

Of course, such a layout is inadequately displayed in the front end. There is a code that changes the source text to the specified $('body').html($('body').html().replace(/исходник/g, 'замена')) . How to expand it so that all possible tag breaks are taken into account and they are replaced with their correct analogues? So that it was possible, or in the script, to sort through all the options, supplement it with new ones, or can something be done using regular expressions?

  • This is definitely a bad idea, because the translator can translate html tags and break everything - andreymal

1 answer 1

If the problem is only in the spaces with which the tags are broken (as you indicated in the example), then this is easy to solve with the help of a simple regular expression.

Try to run the code and change the text in the first field:

 function fInput(oElem) { oElem.nextElementSibling.value = oElem.value.replace(/<\s*(\/)?\s*(.*?\S)\s*>/gi, '<$1$2>'); } 
 textarea { display: block; height: 6em; width: 99%; } 
 <textarea oninput="fInput(this);"><span>fish text</span> они могут выдать < span > рыбный текст< / span> могут выдать < div > рыбный текст< / div > могут выдать < p class="foo"> рыбный текст< / p> < blockquote class="foo_60-90"> рыбный текст< / blockquote></textarea><textarea></textarea>