1
I need to receive an input string for example "Jose_silva", as I do to validate if the string has "_" in it.
1
I need to receive an input string for example "Jose_silva", as I do to validate if the string has "_" in it.
3
index() - returns the position of the first occurrence of a specified value in a string, otherwise returns -1
.
var string = "Jose_Silva";
if (string.indexOf('_') > -1)
{
console.log("contém");
}
You can also use the method includes
of ES6
The method
includes()
determines whether a string can be found inside another string, returning true or false. The methodincludes()
is case sensitive.
var str = 'Jose_Silva';
console.log(str.includes('_'));
1
You can use regular expressions,
if (/_/.test(minhaStringAProcurar)) {
alert(minhaStringAProcurar + ' contém _');
}
that facilitate string handling (and understanding the code once you get used to it). For example, if you get an occurrence of _ in your string, you will probably manipulate it, break it into parts, or replace the character with something,
// Com esta linha abaixo, substituo em minhaStringAProcurar
// todas a ocorrências de _ por dólar, e atribuo o resultado
// de volta a minhaStringAProcurar.
minhaStringAProcurar = /_/gm.replace(minhaStringAProcurar, 'dólar')
Regular expressions, like the name says, allow you to represent a string in patterns of occurrence in the well-defined string, with an appropriate language for this. It is a powerful feature, which can save code writing time considerably:
var resultado = '';
var ocorrenciasDeTextoNoHollerith = /\w+/gm.match(hollerithOriginal)
for (var i = 0; i < ocorrenciasDeTextoNoHollerith.length; ++i) {
if (resultado !== '') resultado += ', ';
resultado += ocorrenciasDeTextoNoHolerith[i];
}
alert(resultado);
Browser other questions tagged javascript jquery
You are not signed in. Login or sign up in order to post.
If possible explain your solution
– Costamilam