Skip to content

Instantly share code, notes, and snippets.

@manchuwook
Created May 12, 2023 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manchuwook/b558893527b81861a1c0a9e92449022e to your computer and use it in GitHub Desktop.
Save manchuwook/b558893527b81861a1c0a9e92449022e to your computer and use it in GitHub Desktop.
Regex for tags
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 "&lt;";
} else if (match === ">") {
return "&gt;";
}
}
// Encode the non-XML tag characters
const encodedText = text.replace(regex, encodeNonXmlTags);
console.log(encodedText);
// Output: "<example>Some text with <tag> and &gt; 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