Hugo Vazquez Asked: 2020-01-21 20:30:24 +0800 CST 2020-01-21 20:30:24 +0800 CST 2020-01-21 20:30:24 +0800 CST How to make a Regular Expression that doesn't allow leading white space? 772 I'm looking for a regular expression that doesn't allow me a leading white space and after ones, yes, it does. javascript 1 Answers Voted Best Answer Mariano 2020-01-21T23:04:05+08:002020-01-21T23:04:05+08:00 Three options: 1. Match a leading whitespace, but negate the entire result: if ( !/^\s/.test(texto) ) { ^^^ el signo de admiración niega al resultado del método .test() ^- Matches the position at the beginning of the text. \s- (lowercase) matches 1 blank, ie [ \t\n\r\f]. Make no mistake, the exclamation mark is the JavaScript logical operatornot , it is not part of the regular expression. 2. Match a non- blank character: /^\S/ ^- Matches the position at the beginning of the text. \S- (Uppercase) matches 1 character that is not , that is \s, [^ \t\n\r\f]. The against: it does not accept empty texts. 3. Use a negative lookahead: /^(?!\s)/ ^- Matches the position at the beginning of the text. (?!.. )- It is a negative lookahead , that is, without consuming characters, it matches only if the pattern inside does not match. \s- Matches a blank space. That is, all this regex can be interpreted as: matches the starting position of the text if it is not followed by a white space Code (with repeated structures to simplify it): const regex1 = /^\s/; const regex2 = /^\S/; const regex3 = /^(?!\s)/; let input1 = document.getElementById("inp1"); let input2 = document.getElementById("inp2"); let input3 = document.getElementById("inp3"); let resultado1 = document.getElementById("res1"); let resultado2 = document.getElementById("res2"); let resultado3 = document.getElementById("res3"); //cuando cambia el texto del input input1.addEventListener("input", function() { //ver si NO coincide el regex /^\s/ if (!regex1.test(input1.value)) { resultado1.innerText = "(no coincide) -> Verdadero :-)"; } else { resultado1.innerText = "(coincide) -> ¡Falso!"; } }); input2.addEventListener("input", function() { //ver si coincide el regex /^\S/ if (regex2.test(input2.value)) { resultado2.innerText = "Coincide :-)"; } else { resultado2.innerText = "¡No coincide!"; } }); input3.addEventListener("input", function() { //ver si coincide el regex /^(?!\s)/ if (regex3.test(input3.value)) { resultado3.innerText = "Coincide :-)"; } else { resultado3.innerText = "¡No coincide!"; } }); <p>Prueba negando a <code>/^\s/</code>:</p> <input type="text" id="inp1"> <code id="res1" r></code> <p>Prueba de <code>/^\S/</code>:</p> <input type="text" id="inp2"> <code id="res2"></code> <p>Prueba de <code>/^(?!\s)/</code>:</p> <input type="text" id="inp3"> <code id="res3"></code>
Three options:
1. Match a leading whitespace, but negate the entire result:
^
- Matches the position at the beginning of the text.\s
- (lowercase) matches 1 blank, ie[ \t\n\r\f]
.Make no mistake, the exclamation mark is the JavaScript logical operator
not
, it is not part of the regular expression.2. Match a non- blank character:
^
- Matches the position at the beginning of the text.\S
- (Uppercase) matches 1 character that is not , that is\s
,[^ \t\n\r\f]
.The against: it does not accept empty texts.
3. Use a negative lookahead:
^
- Matches the position at the beginning of the text.(?!
..)
- It is a negative lookahead , that is, without consuming characters, it matches only if the pattern inside does not match.\s
- Matches a blank space.That is, all this regex can be interpreted as: matches the starting position of the text if it is not followed by a white space
Code (with repeated structures to simplify it):