Reputation: 1345
There are a lot of regex out there but I couldn't find the solution to my specific problem.
Consider these strings:
my street 13a
mystreet 3 B
my street karlos 15A
How can I return those:
my street 13
mystreet 3
my street karlos 15
\d+[a-z,A-Z]
matches the case "13A, 13a" and \d+ [a-z,A-Z]
matches it with whitespaces. After this I would just use this:
myCutStr = myOriginalString.match(/\d+[a-z,A-Z]/g);
myNewStr = myCutStr.replace(/[^\d]/g, '');
myOriginalString.replace(myCutStr, myNewStr);
But can I somehow combine \d+[a-z,A-Z]
and ignore white spaces in that regEx? So that this one \d+[a-z,A-Z]
will match all of my kind even with whitespaces?
Upvotes: 0
Views: 3152
Reputation: 174844
A single replace would be enough. That is, replace all the non-digit characters present at the last with an empty string.
string.replace(/\D+$/g, '');
OR
string.replace(/(\d)\D+$/g, '$1');
Upvotes: 3