Alex Man
Alex Man

Reputation: 4886

Regular Expression for removing numbers

can anyone please tell me how to remove the last numbers before the space using Regular Expression in javascript

ABCD 154 => ABCD  
ACR 15 => ACR   
SA 12S 8945 => SA 12S  

Upvotes: 2

Views: 68

Answers (2)

hwnd
hwnd

Reputation: 70732

Use the end of string $ anchor to replace the preceding space and numbers that follow at the end of the string.

str = str.replace(/ [0-9]+$/, '');

If you want to retain the space character, just remove it from the regular expression.

Working Demo

Upvotes: 3

Yury Tarabanko
Yury Tarabanko

Reputation: 45121

Try

'SA 12S 8945'.replace(/\s\d+$/, '')

Upvotes: 2

Related Questions