Using regex, I want to match all non-word characters at the end of a line, or, if there isn't a non-word character, just match the end of the line.
This is what I thought it should be as simple as:
/\W*$/
That is, match zero or more non-word characters, followed by the end of the line.
I tried it at http://gskinner.com/RegExr/ but it's not that simple.
Then, taking it one step further, I also want to match white space at the end of the line, like this:
/[\W\s]*$/
That is, match zero or more characters from the set, non-word characters and white space characters, followed by the end of the line.
Of course, this isn't working either.
At the end of the day I want to replace anything at the end of a line that is not an alphanumeric character with a full stop, using javascript.
Use the .replace
method with a direct regex, not a string. The ^
is the "not" character. So anything NOT a alphanumeric.
var good = bad.replace(/[^\w]*$/g, "");
Proof:
> bad = "asd$f "
"asd$f "
> bad.replace(/[^\w]*$/g, "")
"asd$f"
My first approach was correct. It was just the regex tool that wasn't playing nicely.
Either /\\W*$/
Or /[^\\w]*$/
Which are obviously the same.
Tested it using this jQuery terminal.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.