-5

How to use regular expressions in strings to select those characters.

const text = `<p height="100" width="300"><span><font color="blue" face="Times New Roman">....some content....</font></span></p>`;

The string I want to select is Times New Roman

2dubbing
  • 1
  • 4

1 Answers1

0

Rather than using regular expressions, use querySelector to navigate to the element with the attribute you want to extract, and then use getAttribute:

const font = document.querySelector('font[color="blue"]');
console.log(font.getAttribute('face'));
<p height="100" width="300">
 <span><font color="blue" face="Times New Roman">....some content....</font></span>
</p>

If your input is a string rather than an HTML element, use DOMParser to turn it into a parsable document first:

const input = `<p height="100" width="300">
 <span><font color="blue" face="Times New Roman">....some content....</font></span>
</p>`;
const doc = new DOMParser().parseFromString(input, 'text/html');
const font = doc.querySelector('font[color="blue"]');
console.log(font.getAttribute('face'));
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • I want to solve it using regular expressions(regex). – 2dubbing Sep 16 '19 at 07:35
  • Regular expressions are [not a good tool](https://stackoverflow.com/a/1732454/) for parsing HTML. Since you're already in a Javascript environment, you should use the Javascript tools already at your disposal for parsing HTML instead. – CertainPerformance Sep 16 '19 at 07:36