I need to remove fractional addresses from a street address input line. I need to allow for people who enter strings like the following:
- 1600 1/2 Main St
- 1600 3/4 Main Street
- 1600.5 Main St
- 1600.75 3rd Street
The above examples should be output as follows:
- 1600 Main St
- 1600 Main Street
- 1600 Main St
- 1600 3rd Street
Here's what I've started with:
var ele = document.getElementById('billing_address_1');
if (typeof(ele) != 'undefined' && ele != null) {
function fractionalAddress() {
originalStreet = ele.value;
//
//var punctuationless = originalStreet.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");
//var finalStreet = punctuationless.replace(/\s{2,}/g," ");
finalStreet = originalStreet.replace(/\s.*\//, '');
ele.value = finalStreet;
console.log(originalStreet);
console.log(finalStreet);
};
ele.addEventListener("change", fractionalAddress);
}
I haven't quite figured out how to grab the character after the slash but before the space. So, the above converts "1600 3/4 Main Street" into "16004 Main Street." Close, but not quite there!
What am I missing? Would it be better to split this into an array?
I need to remove fractional addresses from a street address input line. I need to allow for people who enter strings like the following:
- 1600 1/2 Main St
- 1600 3/4 Main Street
- 1600.5 Main St
- 1600.75 3rd Street
The above examples should be output as follows:
- 1600 Main St
- 1600 Main Street
- 1600 Main St
- 1600 3rd Street
Here's what I've started with:
var ele = document.getElementById('billing_address_1');
if (typeof(ele) != 'undefined' && ele != null) {
function fractionalAddress() {
originalStreet = ele.value;
//https://stackoverflow./questions/4328500/how-can-i-strip-all-punctuation-from-a-string-in-javascript-using-regex
//var punctuationless = originalStreet.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");
//var finalStreet = punctuationless.replace(/\s{2,}/g," ");
finalStreet = originalStreet.replace(/\s.*\//, '');
ele.value = finalStreet;
console.log(originalStreet);
console.log(finalStreet);
};
ele.addEventListener("change", fractionalAddress);
}
I haven't quite figured out how to grab the character after the slash but before the space. So, the above converts "1600 3/4 Main Street" into "16004 Main Street." Close, but not quite there!
What am I missing? Would it be better to split this into an array?
Share Improve this question asked May 3, 2018 at 14:54 paulmiller3000paulmiller3000 46110 silver badges27 bronze badges 2- i thing you will need lookaheads – zedling Commented May 3, 2018 at 14:56
- weird you would remove that from a valid address..... Add numbers to your regexp – epascarello Commented May 3, 2018 at 15:03
2 Answers
Reset to default 5This works for me with the help of regex101.
var addresses = `1600 1/2 Main St
1600 3/4 Main Street
1600.5 Main St
1600.75 3rd Street`
console.log(
addresses.replace(/ ?\d\/\d?|\.\d{1,}/g,"")
)
breakdown:
This regex seems to work.
finalStreet = originalStreet.replace(/\s*(\d+\/\d+|\.\d+)\s/g, '');
A tip for the future, you can use a site like regex101. to test and edit your regex