Most efficient javascript leading/trailing space checker

To vaildate name fields, I need to check that there are no two spaces in a row, and no leading or trailing spaces at all.

I've put together these two methods.  Which is more efficient? Or is there an even more efficient alternative?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>

<BODY>
<script>

name = 'j  onny g ';

//method 1
if (name.indexOf('  ') != -1 ||
    name.indexOf(' ') == 0 ||
    name.indexOf(' ') == name.length-1 )
  document.write('failed1');
else
  document.write('passed1');

document.write('<br>');

//method 2
if (name.indexOf('  ') != -1 ||
    name != name.replace(/^\s*|\s*$/g,""))
  document.write('failed2');
else
  document.write('passed2');

</script>
</BODY>
</HTML>
LVL 5
crimson117Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pravin AsarPrincipal Systems EngineerCommented:
<script language="javascript">
var str = '      Leading ...             ..    Trailing ....         ';
alert ('STR <' + str + '>');
// trim
var str2 = str.replace(/^(\s{1,})|(\s{1,})$/g, '');

// replace two spaces with one space

var str2 = str2.replace (/(\s{2,})/g, ' ');
alert ('STR <' + str2 + '>');
</script>
crimson117Author Commented:
That's a way to strip leading, trailing, and to convert 2 spaces to 1 space.

I'm looking for an answer on what method of checking is most efficient?

Is indexOf faster, or name.replace(/^\s*|\s*$/g,"") faster?
Pravin AsarPrincipal Systems EngineerCommented:
You can use any of the match

I will say this is efficient

if (str.match (/^(\s{1,})|(\S{1,})(\s{2,})(\S{1,})|(\s{1,})$/))
{
alert ('Found Either\n Leading Spaces\n or \nMore than 2 spaces.\n or \nTrailing space\n');
}



<script language="javascript">
var str = '      Leading ...             ..    Trailing ....         ';
alert ('STR <' + str + '>');
if (str.match (/^(\s{1,})/)) { alert ('Found Leading spaces'); }

if (str.match (/(\s{1,})$/)) { alert ('Found Trailing spaces'); }

if (str.match (/(\S{1,})(\s{2,})(\S{1,})/)) { alert ('Found More than 2 spaces.'); }

if (str.match (/^(\s{1,})|(\S{1,})(\s{2,})(\S{1,})|(\s{1,})$/))
{
alert ('Found Either\n Leading Spaces\n or \nMore than 2 spaces.\n or \nTrailing space\n');
}

// trim

//var str2 = str.replace(/^(\s{1,})|(\s{1,})$|(\s{2,})/g, '');

// replace two spaces with one space

//alert ('STR <' + str2 + '>');
Pravin AsarPrincipal Systems EngineerCommented:
>> I've put together these two methods.  Which is more efficient? Or is there an even more efficient alternative?
>>That's a way to strip leading, trailing, and to convert 2 spaces to 1 space.
>>I'm looking for an answer on what method of checking is most efficient?
>>Is indexOf faster, or name.replace(/^\s*|\s*$/g,"") faster?


Do match is much faster and efficient. than any of your method (indexOf or replace),

This one line of code, do all the job..

if (str.match (/^(\s{1,})|(\S{1,})(\s{2,})(\S{1,})|(\s{1,})$/))
{
alert ('Found Either\n Leading Spaces\n or \nMore than 2 spaces.\n or \nTrailing space\n');
}


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
crimson117Author Commented:
>>Do match is much faster and efficient. than any of your method (indexOf or replace),

Perfect, thanks Pravinasar!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.