Is there any way to do strstr () (which exists in php) in jQuery?
Do I need to create a function?
I want to be checked for the exact string I'm passing in another string, for example:
if(strstr("abc", "abcdefgh")){
...
}
Is there any way to do strstr () (which exists in php) in jQuery?
Do I need to create a function?
I want to be checked for the exact string I'm passing in another string, for example:
if(strstr("abc", "abcdefgh")){
...
}
You do not need jQuery for this, just pure javascript, which already contains the indexOf
function, example.
var str = "Hello world, welcome to the universe.";
var n = str.indexOf("welcome");
if (n > -1) alert('Termo encontrado');
else alert('Termo não encontrado');
jQuery is a cross-browser JavaScript library designed to simplify client side scripts that interact with HTML. Wikipedia
Use indexOf
of javascript that returns the position of a string in another. If the return of indexOf
is -1
it means that the first string is not within the second.
if("string que contém".indexOf("string contida") < -1){
/* O que fazer se não encontrar a string */
}
else{
/* O que fazer se a string for encontrada. */
}
Yes, you will need to create a function.
<script type="text/javascript">
var chave = /vai/;
var string = "Como vai Amancio";
var resultado = string.search(chave);
if(resultado != -1){
alert("Encontrado: " + resultado);
}
else{
alert("Não foi possível encontrar");
}
</script>
Hugs!