I have a html string like this which response from API:
const response = `
<html>
<head>
<title></title>
</head>
<body>
<code>Value I want to get<br></code>
</body>
</html>
`;
So the content maybe dynamic but the tag <code>
is unique. I want to have some more solutions on this to see what is the best solution.
You could use a DOMParser
to convert your HTML string into a Document object which you can then use querySelector()
to get your desired value:
const response = ` <html> <head> <title></title> </head> <body> <code>Value I want to get<br></code> </body> </html> `; const {body} = new DOMParser().parseFromString(response, 'text/html'); const value = body.querySelector('code').innerText; // find <code> tag and get text console.log(value);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.