-
-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add html element extractor #229
base: main
Are you sure you want to change the base?
Conversation
We can definitely add this, but I disagree with this expectation:
Ideally, Regex should NOT be used at all for the tests. In that one case, it is forcibly used, because the browser changes the html in the preview. |
Yeah ideally it shouldn't. |
Co-authored-by: Naomi the Technomancer <[email protected]>
I also hopefully will find a way to convert this from Regex into something that uses string manipulation methods. |
* Extracts the inner html of every element inside the head | ||
* @param {String} code a HTML string of the head | ||
* @returns {String} the inner html of every element in the head or an empty string if no head is found | ||
*/ | ||
|
||
export function extractHTMLElement(code: string, tag: string = "head"): string { | ||
const expression = new RegExp( | ||
"(?<=<" + tag + "\\s*>)(?:.|\\s*)*?(?=<\\/" + tag + "\\s*>)" | ||
); | ||
return code.match(expression)?.toString() ?? ""; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The description, params, and def do not match.
Is this just for head
elements? By the look of things, this is for all non-void elements provided they have no attributes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was originally designed for that but I wanted it to be more useful so that I can justify adding it to the library.
|
||
export function extractHTMLElement(code: string, tag: string = "head"): string { | ||
const expression = new RegExp( | ||
"(?<=<" + tag + "\\s*>)(?:.|\\s*)*?(?=<\\/" + tag + "\\s*>)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"(?<=<" + tag + "\\s*>)(?:.|\\s*)*?(?=<\\/" + tag + "\\s*>)" | |
"(?<=<" + tag + "[^>]*>)(?:.|\\s*)*?(?=<\\/" + tag + "\\s*>)" |
Checklist:
Update index.md
)Closes #XXXXX
A variation of this particular regex was used in PRs like freeCodeCamp/freeCodeCamp#55547. I expect this kind of thing to come up a little bit more often in the future which is why I'm adding it to your curriculum helpers. I've also generalized this in an attempt to increase the usefulness. Though without that extra parameter present it will just extract head elements.