Compare two strings .........

W

whisher

Hi.
I've got this simple snippets.
/*function isTheSame(string1,string2)
{
return (string1.toString() == string2.toString())
}*/
function isTheSame(string1,string2)
{
string1 = new RegExp(string1)
return string1.test(string2);
}
alert(isTheSame('pippo','pippo'));
Do you have any suggestion for improvement ? ;)
Take care.
 
A

ASM

whisher a écrit :
Hi.
I've got this simple snippets.
/*function isTheSame(string1,string2)
{
return (string1.toString() == string2.toString())
}*/
function isTheSame(string1,string2)
{
string1 = new RegExp(string1)
return string1.test(string2);
}
alert(isTheSame('pippo','pippo'));
Do you have any suggestion for improvement ? ;)
Take care.

<script type="text/javascript">
function compare(s1, s2) { return s1===s2; }
pipo = 'popi';
</script>

<a href="#"
onclick="alert(compare(123, '123'));
return false;">test numbers ko</a>
<a href="#"
onclick="alert(compare(pipo, 'pipo'));
return false;">test variable ko</a>
<a href="#"
onclick="alert(compare('pipo', 'pipo'));
return false;">test strings ok</a>
<a href="#"
onclick="alert(compare(pipo, 'popi'));
return false;">test variable ok</a>
 
M

mick white

whisher said:
function isTheSame(string1,string2)
{
string1 = new RegExp(string1)
return string1.test(string2);
}
alert(isTheSame('pippo','pippo'));

alert(isTheSame('pippo','ripippotorasaurus'));

Clearly they're not the same.
Mick
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>,
alert(isTheSame('pippo','ripippotorasaurus'));

Clearly they're not the same.

But

function isTheSame(string1, string2) {
return new RegExp(string1).test(string2) && new
RegExp(string2).test(string1)}

works better. Wouldn't use it myself, though, except to confuse.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top