Tony
Tony

Reputation: 79

Regex - stripping the first number or number and ajoining letter

I'm attempting to remove the house number from an address and have successfully managed to remove the numbers, but I am stuck with examples that include a letter with the number, i.e. 12a The Street.

Here's my code:

var pattern = /\d+(?!.*\d)([^]*)/;

$('#myTextbox').change(function () {
   strExport = ($('#_myTextbox').val().match(pattern) || [, $('#myTextbox').val()])[1];
   $('#myTextbox2').val($.trim(strExport))
});

I did manage to get the Regex working to remove the 12a, but then I broke it for pure number examples.

var pattern = /\d+-?[a-zA-Z]+(?!.*\d)([^]*)/;

Thanks.

Upvotes: 0

Views: 375

Answers (1)

MDEV
MDEV

Reputation: 10838

It can be much simpler than that:

var str = $('#_myTextbox').val();//"12a The Street"
str = str.replace(/^\w+\s+/,'');//"The Street"

Or for fixed number then optional letters format, use this regex instead:

/^\d+[a-z]*\s+/

FYI:

  • \w is a 'word character', equating to [a-zA-Z0-9_]
  • \d is a digit, equating to [0-9]

Upvotes: 1

Related Questions