最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Javascript replace with regex not working correctly - Stack Overflow

programmeradmin1浏览0评论

I'm trying to validate a name with regex, the regex stops the user from entering 2 spaces or dots in a row.

This is my code:

function test(input) {
  var regex = /^[A-Za-z]+\.{0,1}\s{0,1}$/;
  input.value = input.value.replace(regex, "");
}
<input id="txt_NomCandidato" onkeyup="test(this);" type="text" class="form-control" name="txt_Nom">

I'm trying to validate a name with regex, the regex stops the user from entering 2 spaces or dots in a row.

This is my code:

function test(input) {
  var regex = /^[A-Za-z]+\.{0,1}\s{0,1}$/;
  input.value = input.value.replace(regex, "");
}
<input id="txt_NomCandidato" onkeyup="test(this);" type="text" class="form-control" name="txt_Nom">

At the moment I want to enter a letter, I cant I don't know why, I hope you guys can help.

EDIT: Name only contains letters, not numbers.

Share Improve this question edited Apr 12, 2018 at 16:21 Yoset GA asked Apr 12, 2018 at 15:41 Yoset GAYoset GA 391 silver badge13 bronze badges 3
  • FYI {0,1} is the same as ? – ctwheels Commented Apr 12, 2018 at 15:49
  • 1 Your regex matches what I assume you actually want to allow. Since you are then in turn using it to replace, it removes anything that matches it. – Josh Mein Commented Apr 12, 2018 at 15:52
  • ... the regex stops the user from entering 2 spaces or dots in a row. it's not clear if you are trying to avoid two spaces or not. – revo Commented Apr 12, 2018 at 16:07
Add a ment  | 

6 Answers 6

Reset to default 2

With this solution it deletes only the second space or second dot and the first ones will still remain:

function test(input){
  var regex=/\.(?=\.)|\s(?=\s)/;
  input.value = input.value.replace(regex, "");
}
<input id="txt_NomCandidato" onkeyup="test(this);" type="text" class="form-control" name="txt_Nom">

The following regex should work for you: /[^a-zA-Z. ]|\.(?=\.)|\s(?=\s)/g

  • [^a-zA-Z. ]+ Limits all allowed characters to a-z, A-Z, dots, or spaces
  • \.(?=\.) Limits it to only allow one dot in a row
  • \s(?=\s) Limits it to only one space in a row

Each of these are separated by | (the or condition)

function test(input) {
  var regex = /[^a-zA-Z. ]|\.(?=\.)|\s(?=\s)/g;
  input.value = input.value.replace(regex, "");
}
<input id="txt_NomCandidato" onkeyup="test(this);" type="text" class="form-control" name="txt_Nom">

Your regex seems correct (it matches any string with no more than 1 space or 1 dot together), but your the logic in your function seems wrong.

What it's doing: anytime the value changes, take the value and replace anything that matches that regex with an empty string, so any time you write a character its replaced (because it matches the regex).

I would go with one of this options:

a) show a message if the value doesn't match the regex

b) change the regex to /.{2}| {2}/ and only replace the two dots or the two spaces, as Scath says in another answer

c) maybe just using the input pattern attribute with your original regex is enough: https://www.w3schools./TAGS/att_input_pattern.asp

Hope this helps!

To achieve expected result, use below option of slice

input.value.slice(-2) will return last two entered characters and if it is ".." or " "(double spaces) replace it with ''

function test(input){
  if(input.value.slice(-2)=='..' || input.value.slice(-2)=='  '){
         input.value = input.value.replace(input.value.slice(-2), "");
     }
}
<input id="txt_NomCandidato" onkeyup="test(this);" type="text" class="form-control" name="txt_Nom">

code sample - https://codepen.io/nagasai/pen/VXNOej

Your regex clears any characters starts with [A-Z] or [a-z] due to expression ^[A-Za-z]

This regex will not allow them to enter two periods or two spaces, when they enter the second of either it will clear.

function test(input){
  var regex=/\.{2}| {2}/;
  input.value = input.value.replace(regex, "").replace(/\d+|/g, '').replace(/\s+/g, ' ').replace(/[^\w]/, '');
}
<input id="txt_NomCandidato" onkeyup="test(this);" type="text" class="form-control" name="txt_Nom">

That is because you match [A-Za-z]+ which will match one or more characters and everything after that is optional.

Your regex would match.

  • [A-Za-z]+ Match one or more characters
  • \.{0,1} Match optional dot (0 or 1 times)
  • \s{0,1} match optional whitespace character (0 or 1 times)

Which will match a and then you will do the replace.

To make something optional you could also use a question mark ?

To only allow characters, a single whitespace or a single dot you could replace all that does not match those criteria:

([. ])(?=\1)|[^a-zA-Z. ]

function test(input) {
  var regex = /([. ])(?=\1)|[^a-zA-Z. ]/;
  input.value = input.value.replace(regex, "");
}
<form id="" name="">
  <input id="txt_NomCandidato" onkeyup="test(this);" type="text" class="form-control" name="txt_Nom">
</form>

To match a dot followed by a dot or a white space followed by a white space you could capture a dot or a white space in a group and it will match if it is followed by what is captured in the group using a positive lookahead.

([. ])(?=\1)

发布评论

评论列表(0)

  1. 暂无评论