Let's say i have this string :
<img src="./img-apple-64/1f525.png" alt="??">This is only<img src="./img-apple-64/1f525.png" alt="??"> <img src="./img-apple-64/1f916.png" alt="??">what i want<img src="./img-apple-64/1f916.png" alt="??">
I need to extract only the part of the text without the <img tag. In the example above, this is the resuilt i need :
This is only what i want
Is there a fast / simple way to do this, without having to loop the entire string and manually replacing things ?
CodePudding user response:
Use DOM?
const str = `<img src="./img-apple-64/1f525.png" alt="??">This is only<img src="./img-apple-64/1f525.png" alt="??"> <img src="./img-apple-64/1f916.png" alt="??">what i want<img src="./img-apple-64/1f916.png" alt="??">`
const div = document.createElement("div");
div.innerHTML = str
console.log(div.textContent)
CodePudding user response:
The following regular expression:
/(?<=\>)([^\<][\s\w]{1,})/gm
will do this for you. See below:
let str = '<img src="./img-apple-64/1f525.png" alt="??">This is only<img src="./img-apple-64/1f525.png" alt="??"> <img src="./img-apple-64/1f916.png" alt="??">what i want<img src="./img-apple-64/1f916.png" alt="??">'
let match = str.match(/(?<=\>)([^\<][\s\w]{1,})/gm);
match is an array containing the values ['This is only', 'what I want']