admin管理员组文章数量:1334671
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
本文标签: How to remove **** from string in JavaScriptStack Overflow
版权声明:本文标题:How to remove *.*, ** from string in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742372739a2462554.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论