0
Remove unknown amount of unknown characters from string in JS?
I have a strong, something like "balblabla onclick='blabla()' blablabbla", and I have to remove that onclick from the string. The attribute to the function in the onclick will keep changing though, so it's never the same length. How do I go about this? Using regex? I've never used those, so any assistance would be great! Thanks!
6 Answers
+ 1
I modified the expression according to what you said. Could you please have another look at it? I also loosely accounted for arrays, objects and functions as possible arguments. Doesn't look pretty though.
+ 1
Regex is a possibility, the following expression should do the trick:
https://code.sololearn.com/Wcyw6N0ystp3/?ref=app
If you know that the rest of the string contains no single quotation character or bracket, you could also search for them and then use them as an identifier to delete everything between the position of onclick and that position.
+ 1
Shadow yes, that did it! Thank you!
or should I say, Danke!
0
Shadow that would do the trick but the attribute always changes. Like, once the attribute is 'hello' and once it's 'goodbye'. How does that work?
0
If I understood you correctly, only the function name changes? That is covered by the regex expression. Otherwise I got your question wrong I guess.
The expression currently looks for a string containing
onclick,
followed by any amount of whitespaces (\\s*)
followed by =,
followed by any amount of whitespaces again,
followed by ',
followed by any combination of characters and digits (the function name, replace \\w with [_\\w] to enable underscores too),
followed by () and '.
If the brackets are not mandatory, replace
\\(\\) with
(\\(\\))?.
0
Nono, the function call will sometimes look like this func("some") and sometimes like this func("something").
The function name will always be the same, the argument won't.