I’m receiving a string from user for fields lable that can contain special characters
and HTML
tag as well.
Examples:
How will you be attending the event?
select Education Level <i>some instruction</i>
I want to trim the string before any special character. How can it do?
Note: there is no specified special character list. excluded characters are
_,-, +
. Users can provide any of the characters they desire. But we can target commonly used special characters$%&*()}{@#~?><,|=
those are used in questionnaire/instruction statements.
Desire Output:
How will you be attending the event
select Education Level
3
Answers
I used regex expression. It worked for me.
Explanation:
if
condition, I'm checking the special characters existence within the string.If anyone has any suggestions plz let me know.
Try:
and replace with an empty string.
See:
https://regex101.com/r/tWqcfr/latest
https://3v4l.org/lPNUs
Explaination:
Match what you do not need and throw it away
h*
Optionally match any leading horizontal whitespace before …[$%&*()}{@#~?><,|=]
…encoundtering a special symbol….*$
…and match the rest of the string.Try this simple example (demo – https://3v4l.org/tYvI1):
So you will get all words since start row to any special symbol to $matches array. Obviously the array will have onle one element in your case because of the "start row" condition.
Best regards,
Anton