Compare the first character of the text

2

I have a code, that if a function is given as a parameter the string:

#elemento

You will use a document.getElementById();

and if you receive the string of the form:

.elemento

You will use a getElementsByClassName();

But the problem is, what do I do when they send "#."? It would be convenient to take into account only the first character, so I thought about doing:

if(parametro.substr(0,1) == "#" ) { //Hacer algo }

And it would work, but how could I do it with a regular expression?

    
asked by Eduardo Sebastian 05.10.2017 в 02:23
source

3 answers

3

As I mentioned, I would never use regex for something that can be solved easily:

if (parametro[0] == '#') {
    // ...
}

Neither to reinvent a function that already exists: document.querySelector(parametro) .

But, how are you interested to learn ...


In regex, ^ is an assertion that matches the beginning of the string (or the line if a modifier is used). Like any assertion, it does not match a character, but with a position that meets the condition.

Thus, we can use ^ to guarantee that it matches the start and then the desired character. The expression would be:

/^#/
  • A # at the beginning of the text.


Code:

function esId(parametro) {
    return /^#/.test(parametro);
}

//Pruebas
console.log(esId("#abc"));  //true
console.log(esId(".#def")); //false
console.log(esId("#.ghi")); //true


More information on Regular Expressions (MDN) .

    
answered by 05.10.2017 / 02:54
source
0

The apparent objective is to detect the first character, but I see a fundamental problem, and it is to detect the names of the variables.

For this, what I do is, besides looking at the first character, to see that the second is not a number, and also accept other characters such as underscore and weights sign.

These characters are the most common, but at the time of testing, you can accept up to unicode characters. I will not go into that since it is complicated, but I leave an approximate solution.

A variable can not start with a number, and the following can be a number, letters, or whatever, as long as it does not come up against the special characters of the language.

  • So that the first character of the variable is not a number: [^0-9]
  • To make it a letter: [a-z]
  • A number: \d
  • One point: \. ... Here you have to be careful, because the point in some languages is like a wild card, that is, accepts anything.
  • Pad (for this character there is no trick): # .

var nombre_variable="[^0-9][a-z\d_$]{0,}"
new RegExp(nombre_variable,"i")
var almohadilla=new RegExp("^#"+nombre_variable+"$")
var propiedad=new RegExp("^\."+nombre_variable+"$")
console.log(almohadilla)
console.log(propiedad)

var pruebas=[
  "variable_8", "3_a"
]
for(var i in pruebas){

  console.log( "."+pruebas[i]+" "+
    propiedad.test("."+pruebas[i])
  )
  console.log( "#"+pruebas[i]+" "+
    propiedad.test("#"+pruebas[i])
  )
}
    
answered by 05.10.2017 в 03:13
0

From what I understand you need to validate that they do not enter "#.", if so, with the following regular expression you can validate and execute some error condition.

var str="#.element";
var a = str.match(/\#\./);
if(a) {
    // condición de error
    alert(a);
}
    
answered by 05.10.2017 в 04:29