Is it possible to skip html tags using jquery and split method?

I use split to insert javascript generated text into html text, but I need to skip parts like html tags </><\> , so after a lot of searching, I found this:

 var = var.replace(/<\/?[\w#"'-=:; {},.\r\n]+\/?>/g, '\n'); 

the problem is that it does not skip tags or anything that is specified in this variable, it actually replaces the tags with a space.

What I need to achieve, for example:

<script>random javascript</script><p>my text</p> --- New text that needs to be inserted ---

What is currently happening:

my text</p> --- New text that needs to be inserted ---

As you can see, the first tags (The javascript) are not skipped, they are deleted.

Which method should be used instead of replacing?

0
source share
1 answer

to insert javascript generated text into html text

What I need to achieve, for example:

<script>random javascript</script><p>my text</p> --- New text that > needs to be inserted ---

Try using document.createTextNode() , .insertAfter()

 var text = document.createTextNode("--- New text that needs to be inserted ---"); $(text).insertAfter("p:contains(my text)") 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <script>var str = "random javascript"</script><p>my text</p> 
+1
source

Source: https://habr.com/ru/post/1235440/


All Articles