Comparar 2 strings ignorando maiúsculas e minúsculas.

1,070 views
Skip to first unread message

Acaz Souza

unread,
Feb 16, 2009, 12:12:26 PM2/16/09
to jQuery (Brasil)
Alguém consegue me ajudar com alguma função que compare 2 strings
trazendo se são iguais ou não, mas ignorando letras maiúsculas e
minúsculas entre elas?

Acaz Souza

unread,
Feb 16, 2009, 12:17:58 PM2/16/09
to jQuery (Brasil)
Deixa eu explicar melhor:

preciso fazer um replace entre 2 strings, e quero ignorar maisculas e
minusculas

<script type="text/javascript">
$(function() {
var q = $('#ctl00_ContentPlaceHolder1_HD_BUSCA').val();
var t = '';
$('#lista-geral li a span').each(function() {
t = $(this).text();
$(this).html(t.replace(q, '<strong>' + q + '</
strong>'));
})
})
</script>

nesse script isto não acontece.

Pablo

unread,
Feb 16, 2009, 12:24:39 PM2/16/09
to jque...@googlegroups.com
Numn seria apenas transformar ambas em maiusculas ow minusculas e comparar?

Juarez Gonçalves Nery Junior

unread,
Feb 16, 2009, 12:30:54 PM2/16/09
to jque...@googlegroups.com
use o método wrap do jquery

$($(this).text()).wrap(document.createElement('strong'));

[s]


On Mon, Feb 16, 2009 at 2:24 PM, Pablo <pablo...@gmail.com> wrote:
Numn seria apenas transformar ambas em maiusculas ow minusculas e comparar?






Acaz Souza

unread,
Feb 16, 2009, 12:33:54 PM2/16/09
to jQuery (Brasil)
consegui!
fiz da seguinte maneira:

<script type="text/javascript">
$(function() {
var q = $('#ctl00_ContentPlaceHolder1_HD_BUSCA').val();
var e = null;
var t = '';
var tNew = '';
var tLower = '';
var tLength = 0;
var p = '';
var pPrev = '';
var pNext = '';
var sIndex = 0;
var fIndex = 0;

q = q.toLowerCase();
$('#lista-geral li a span').each(function() {
e = $(this);
t = $(e).text();
tLower = t.toLowerCase();
tLength = t.length;

sIndex = tLower.indexOf(q);
fIndex = sIndex + q.length;
if (sIndex != -1) {
p = t.substring(sIndex, fIndex);
p = '<strong>' + p + '</strong>'
pPrev = t.substring(0, sIndex);
pNext = t.substring(fIndex, tLength);
tNew = pPrev + p + pNext;

$(e).html(tNew);
}
})
})
</script>

Peguei o indice de onde ocorre a palavra, assim deu para separar a
frase em 3 partes, antes da palavra, a palavra e depois da palavra.
depois juntei tudo no final com a modificação que queria com a palavra.

Marcelo Andrade

unread,
Feb 16, 2009, 12:37:51 PM2/16/09
to jque...@googlegroups.com
2009/2/16 Acaz Souza <acaz...@gmail.com>:

>
> Alguém consegue me ajudar com alguma função que compare 2 strings
> trazendo se são iguais ou não, mas ignorando letras maiúsculas e
> minúsculas entre elas?

Joga as duas para maíusculas com .toUpperCase()
e compara normal. :-)

Atenciosamente.

--
MARCELO DE F. ANDRADE (aka "eleKtron")
Belem, PA, Amazonia, Brazil
Linux User #221105

[gus@pará ~]# links http://pa.slackwarebrasil.org/

Ricardo Tomasi

unread,
Feb 17, 2009, 12:31:10 PM2/17/09
to jQuery (Brasil)
Dá pra usar uma expressão regular case-insensitive:

$(this).html(t.replace(new RegExp(q, 'i'), '<strong>'+q+'</
strong>'));

- ricardo

Reply all
Reply to author
Forward
0 new messages