Created
May 12, 2023 21:40
-
-
Save manchuwook/b558893527b81861a1c0a9e92449022e to your computer and use it in GitHub Desktop.
Regex for tags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Apologies for the confusion. If you only want to encode `<` and `>` characters that are not part of XML tags, you can modify the regex and the encoding logic. Here's an updated example: | |
```javascript | |
const text = "<example>Some text with <tag> and > symbol</example>"; | |
// Regular expression to match '<' or '>' not followed by an XML tag | |
const regex = /(<(?!\/?[a-zA-Z]+\b)[^>]*>)|([<>])/g; | |
// Function to encode the matched characters | |
function encodeNonXmlTags(match, xmlTag) { | |
if (xmlTag) { | |
return xmlTag; | |
} else if (match === "<") { | |
return "<"; | |
} else if (match === ">") { | |
return ">"; | |
} | |
} | |
// Encode the non-XML tag characters | |
const encodedText = text.replace(regex, encodeNonXmlTags); | |
console.log(encodedText); | |
// Output: "<example>Some text with <tag> and > symbol</example>" | |
``` | |
In this updated example, the regular expression (`regex`) now uses a modified pattern to capture both the XML tags and the `<` and `>` characters. The `encodeNonXmlTags()` function is updated to return the captured XML tags as they are, and encode only the `<` and `>` characters. | |
The resulting `encodedText` will contain the encoded versions of `<` and `>` characters that are not part of XML tags, while leaving the XML tags unchanged. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment