Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match last space character

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.

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.

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

Anybody with ideas? Thanks.

like image 781
Gerald Ferreira Avatar asked Mar 17 '10 16:03

Gerald Ferreira


People also ask

How do you match a space in regex?

\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.

Does regex match dot space?

Does regex match dot space? Yes, the dot regex matches whitespace characters when using Python's re module.

What does \b mean in regex?

The word boundary \b matches positions where one side is a word character (usually a letter, digit or underscore—but see below for variations across engines) and the other side is not a word character (for instance, it may be the beginning of the string or a space character).

What does \\ mean in regex?

The backslash character (\) in a regular expression indicates that the character that follows it either is a special character (as shown in the following table), or should be interpreted literally. For more information, see Character Escapes. Escaped character. Description. Pattern.


1 Answers

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

 str = str.replace(/\s+\S*$/, "")
like image 72
user187291 Avatar answered Sep 19 '22 15:09

user187291