-1

i want to replace text within </> with <strong> tag.

What i am trying to do? conside i have a string i am <some-tag some-id="3" text="fname lname"/> and other name <some-tag some-id="4" text="fname1 lname1"/>

so i want to replace the substring <some-tag some-id="3" text="fname lname"/> to <strong>fname lname</strong>

similarly to substring <some-tag some-id="4" text="fname1 lname1"/> to

<strong>fname1 lname1</strong>

so the final output for string

 i am <some-tag some-id="3" text="fname lname"/> and other name <some-tag 
 some-id="4" text="fname1 lname1"/>

should be

i am <strong>fname lname</strong> and other name <strong>fname1 
lname1</strong>

What i have tried?

let str1 = str.replace(/(<some-tag\b[^>]*>)[^<>]*(<\/>)/g," 
<strong>$1</strong>");

But doesnt seem to work. could someone help me out with this thanks.

Andreas
  • 21,535
  • 7
  • 47
  • 56
  • `<\/>` matches `>` so you did not even try to match self-closing tags. Besides, you are using two capturing groups and the first one is around the `some-tag` element node, so this will be inserted with `$1`, clearly not something you need. Please consider using an XML/HTML DOM parser to achieve what you need. – Wiktor Stribiżew Aug 29 '19 at 07:41
  • eh @WiktorStribiżew , are you really saying that he can use regex to find something within html tags? Eww. Select by tags first, then replace its content with a simple replace .__. – KarelG Aug 29 '19 at 07:41
  • @KarelG [René Datenschutz is](https://stackoverflow.com/a/57705403/3832970). I only stated just some problems OP has with regex. Once people see how many issues they may have with regex HTML parsing, they will understand the DOM parser usefulness much faster. – Wiktor Stribiżew Aug 29 '19 at 07:44
  • have a look at [why not to parse html with regex](https://stackoverflow.com/a/1732454/128165) – Gabriele Petrioli Aug 29 '19 at 07:47
  • i think it can still be done with regex alone since i want to match the pattern like this and get the value from text= and replace that entire thing with "value got from text" – stackoverflow_user Aug 29 '19 at 08:05
  • What if `text` attribute value contains `<` or `>`? – Wiktor Stribiżew Aug 29 '19 at 08:51
  • it will never contain such character – stackoverflow_user Aug 29 '19 at 12:37

1 Answers1

0

maybe like this?

  const attributeHTML = new RegExp('<([\\w-]+).*?>(.*)<\\/\\1>', 'g');
  while ((matches = attributeHTML.exec(content))) {
    console.log(RegExp.$1);
  }

https://regex101.com/r/G24DH7/2

Scriptkiddy1337
  • 792
  • 4
  • 9
  • happens if u try to improve it and post it untested, sorry. it's edited to the unimproved one (which works for me) – Scriptkiddy1337 Aug 29 '19 at 08:26
  • 1
    [Regarding `RegExp.$1`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/n): *This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.* – Wiktor Stribiżew Aug 29 '19 at 08:56