Class of characters denied as [^ +], without escaping the + sign

0

I have 2 patterns:

var a = /[^+][a-zA-Z]/;
var b = /[a-zA-Z][^+]/;

According to sources that I have read, what the a does, is to deny with ^ the character + , but you should not escape it ?, staying:

[^\+]

In addition, the b pattern does the same but in the end, it should not be $[^+] , why $ is for the end? and vice versa with the pattern a , which should be ^[^+] ? Why is not it so?

PD: The question itself is directed to the pattern, even if it does not escape the + , nor does it use ^[^+] ni $[^+] , since that understood the YO operation, of the regex

    
asked by Eduardo Sebastian 13.11.2017 в 23:42
source

2 answers

4

There are several concepts to correct about what you are interpreting:

  • A character class (in brackets) matches one, and only one, character. It does not matter if it is denied or not, it always coincides with a character.

  • Although it is necessary to escape the metacharacters in regex, it is not necessary to escape the majority within a class of characters. The only 3 that need to be escaped in brackets are:

    • ^ when it is at the beginning, to avoid that it denies the class.
    • - in the middle, to prevent it from building a range.
    • \ because it is used to escape other characters.
    • ] to avoid closing the class.

    That is to say that [^+] and [^\+] are exactly the same.

  • The two patterns do not match the same. It's not the same:

    • /[^+][a-zA-Z]/ ::: a character (any except a + ), followed by another alphabetic character, which
    • /[a-zA-Z][^+]/ ::: an alphabetic character followed by another character (any except a + ).

    For example, the first pattern matches the text "@m" , but the second pattern does not. Note how [^+] matches the @ , of course, because it is a character that is not a + .

  • ^ matches the position at the beginning of the text, and $ matches the position at the end of the text. And I'm emphasizing position because that's what you should think about when using them. For example, what you said is not correct: $[^+] means the final position of the text and then any character except a + ... will never coincide, with nothing, it is impossible. The final position of the text can not be followed by another character.

  • Since you are not using ^ or $ , both patterns will try to match 2 characters at any position in the text. For example, both patterns will match the text "+++86gg99+++" , but each will match different parts of that text. Can you guess what 2 characters each will match?

    //Esta es la respuesta
    
    var a = /[^+][a-zA-Z]/,
        b = /[a-zA-Z][^+]/,
        texto = "+++86gg99+++";
    
    console.log(a.source, "coincide con", texto.match(a));
    console.log(b.source, "coincide con", texto.match(b));
answered by 14.11.2017 / 00:38
source
2

First you have to start with what your employer does:

var a = '[^+][a-zA-Z]'
var reg = new RegExp(a);
console.log(reg.test('aA'));
console.log(reg.test('+aA'));

This searches for any match in a string that contains: [cualquier caracter que no sea '+'] and [cualquier caracter que esté entre a-z o A-Z]

Some examples of strings that your pattern will find: 'aF', 'eC', '3V', '.a' In this league you can see the patterns that the expression finds, plus I use it a lot when I work with regular expressions: link

Now your first question: Escape or not the plus sign: [^\+] ? As such it is not necessary since if you do the test both expressions are equivalent and will give you the same result:

var a = '[^\+][a-zA-Z]'
var reg = new RegExp(a);
console.log(reg.test('aA'));

This is mainly because within that expression the plus sign is taken as it is, a plus sign character, and not as a quantifier like a regular expression used to use it, for that this element would have to go to the right of some character where you need to look for one or more, or to the right of an expression where you also need to look for one or more example:

var a = '[^+]+[a-zA-Z]'
var b = '[^+][a-zA-Z]+'
var reg = new RegExp(a);
var regB = new RegExp(b);
console.log(reg.test('aaA'));
console.log(regB.test('icCdfe'));

Here is also the example in the league with pattern b of this example, and if you look now you look for longer strings, for the quantifier + : link

The second question: Use ^ y $ ? These symbols indicate to the expression the beginning and the end of the chain that the expression should look for, what we must emphasize here is that you are giving a beginning and an end, so anything that is before or after your string that you are looking for will cause your expression not to find it when not complying with the patterns, example:

var a = '^[^+][a-zA-Z]$'
var reg = new RegExp(a);
console.log(reg.test('aA'));
console.log(reg.test('+a')); //Caracter '+' por lo que falla
console.log(reg.test('+aA')); //Caracter '+' pero usas ^$ por lo que aún falla, aunque en los primeros ejemplos si funcione

With this we can say that when using ^ $ you set the search pattern to a result and that it is exact and meets the condition of the expression, not to use it as in the first examples, even if it finds a plus sign , keep finding a couple of characters that meet your condition and the test returns you true

    
answered by 14.11.2017 в 00:36