-2

In my React project I have a string like this:

<App>ew <View id="view1" visible="true">4</View>- <View id="view1" visible="true" width="100">WORD</View>s</App>

and I'm trying to remove any symbols and words outside tags even if the document is valid XML.

The result should be like this:

<App><View id="view1" visible="true"></View> <View id="view2" visible="true" width="100"></View></App>

May be there's another way without using RegExp, I'd appreciate any help

Vit
  • 21
  • 2
  • You are trying to do pattern matching, which is what regex is for. You could do iterative string replacement, but it would be far less efficient, both in terms of memory and execution time. I can't think of another solution to pattern matching and replacing. Why are you hesitant to use regex? – Andy Jan 30 '21 at 20:47

1 Answers1

0

const regex = /(^|>)[^<]+/g;
const test = `abcd<App>ew <View id="view1" visible="true">4</View>- <View id="view1" visible="true" width="100">WORD</View>s</App>abcd`;

console.log(test.replace(regex, "$1"));

I really like regex101.com for figuring things like this out. It has a searchable quick reference and also an explanation of how your current regex is working and what it's matching.

This one matches either the start of the string or a literal > character in a group, followed by 1 or more characters that are not <. It uses the global regex option to allow it to find all matches instead of stopping after the first one.

Then you replace all matches found a reference to group 1 ($1). This handles anything before the first element, after the last element, or in between XML elements.

Here is a link to regex101.com with this regular expression so you can see their explanation, since it will be better than mine.

https://regex101.com/r/NgiyTZ/1

Andy
  • 648
  • 8
  • 21
  • Updated my answer and regex101 saved expression to handle text before the first element. – Andy Jan 30 '21 at 19:47