Javascript 正则表达式匹配最后一个空格字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2464021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:28:52  来源:igfitidea点击:

Regex to match last space character

javascriptregexasp-classic

提问by Gerald Ferreira

I need some help. I am looking for a regex that would match the last space character in a string. I am using JavaScript and classic ASP.

我需要帮助。我正在寻找一个匹配字符串中最后一个空格字符的正则表达式。我正在使用 JavaScript 和经典的 ASP。

I have a long string of text which I trim to 100 characters. I would like to remove the last character to avoid a spelling mistake if the trim cuts a word due to the 100 characters limit.

我有一长串文本,我将其修剪为 100 个字符。如果修剪由于 100 个字符的限制而削减了一个单词,我想删除最后一个字符以避免拼写错误。

regex.replace(/[ ]$.*?/ig, '');

Anybody with ideas? Thanks.

有人有想法吗?谢谢。

回答by user187291

From my understanding, you need to remove the last space and everything after it, right?

根据我的理解,您需要删除最后一个空格及其后的所有内容,对吗?

 str = str.replace(/\s+\S*$/, "")

回答by Gumbo

Try a lookahead assertion:

尝试先行断言

/ (?=[^ ]*$)/

And for arbitrary whitespace characters:

对于任意空白字符:

/\s(?=\S*$)/

回答by Tomalak

That would be

那将是

/ +$/

A space character (), at least once (+), at the end of the line ($)

空格字符 ( ),至少一次 ( +),在行尾 ( $)

Note that putting the space in a character class is unnecessary, unless you want to match more than a space character, for example tabs [\t ].

请注意,将空格放在字符类中是不必要的,除非您想匹配多个空格字符,例如 tabs [\t ]

To really match the single last spaceonly, use / $/

要真正匹配最后一个空格,请使用/ $/



EDIT: To cut off everything after the last space (thinking about it, this is what you actuallyseem to want, you can use:

编辑:要切断最后一个空格后的所有内容(考虑一下,这就是您真正想要的,您可以使用:

regex.replace(/ +\S*$/ig, '');

where the regex means: "At least one space, and any non-whitespace characters after it (\S*), at the end of the line ($).".

其中正则表达式的意思是:“至少一个空格,以及它之后的任何非空白字符 ( \S*),在行的末尾 ( $).”。

This can only match the last bit of a line after the last space. As a side-effect, the string is trimmed at the end.

这只能匹配最后一个空格后一行的最后一位。作为副作用,字符串在末尾被修剪。

回答by zellio

The regex /^.+ [^ ]*$/will match only the last space of a line.

正则表达式/^.+ [^ ]*$/将只匹配一行的最后一个空格。

回答by Jerry Coffin

Using "classic" REs, what you'd want would be " [^ ]*$"-- i.e. a space character followed by an arbitrary number of non-space characters followed by the end of the line. The "non-space characters" might or might not fit your definition of a "word" though -- for example, it'll also prevent you from cutting a number in the middle. If you want to limit a "word" to containing letters, you could use: " [^A-Za-z]*$"instead.

使用“经典”RE,您想要的是" [^ ]*$"——即一个空格字符后跟任意数量的非空格字符,然后是行尾。“非空格字符”可能符合也可能不符合您对“单词”的定义——例如,它还会阻止您在中间切割一个数字。如果要将“单词”限制为包含字母,则可以使用:" [^A-Za-z]*$"代替。

Also note that either way, if a word happens to end at exactly the 100th character, this will match the space before it, so you'll remove that last entire word. If you want to prevent that, you'll (probably) want to look at the first character that you cut off, and if it's white space, don't remove anything from the buffer.

另请注意,无论哪种方式,如果一个单词恰好在第 100 个字符处结束,这将匹配它前面的空格,因此您将删除最后一个整个单词。如果您想防止这种情况发生,您(可能)会想要查看您切断的第一个字符,如果它是空格,请不要从缓冲区中删除任何内容。