• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 766
  • Last Modified:

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>
0
crimson117
Asked:
crimson117
  • 3
  • 2
1 Solution
 
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>
0
 
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?
0
 
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 + '>');
0
 
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');
}


0
 
crimson117Author Commented:
>>Do match is much faster and efficient. than any of your method (indexOf or replace),

Perfect, thanks Pravinasar!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now