Page 1 of 1

Help: regular expression: removing white spaces

Posted: Fri Jul 15, 2005 7:23 am
by raghavan20
I am trying to remove the white space in the 'phone' field, which i am not able to do with the following function:

phone = phone.replace(/^[\s]+/g,"");

You can see the form in action at:
http://www.omnisoft.co.uk/callMeBack.ph ... rmCallBack

Posted: Fri Jul 15, 2005 9:12 pm
by Sphen001
Hi,

Why not just try a simple preg_replace?

Code: Select all

<?php
$phone_num = preg_replace(" ", "", $input);
?>
Hope this helps :D

Sphen001

Code: Select all

<?php
$phone_num = preg_replace(" ", "", $input);
?>
Hope this helps :D

Sphen001one_num = preg_replace(" ", "", $input);
?>

Hope this helps :D

Sphen001quote;", $input);
?>

Hope this helps :D

Sphen001; ", "", $input);
?>

Hope this helps :D

Sphen001t try a simple preg_replace?

Code: Select all

<?php
$phone_num = preg_replace(&quote; &quote;, &quote;&quote;, $input);
?&t try a simple preg_replace?

Code: Select all

<?php
$phone_num = preg_replace(" ", "", $input);
?>
Hope this helps :D

Sphen001hone_num = preg_replace(" ", "", $input);
?>

Hope this helps :D

Sphen001(" ", "", $input);
?>

Hope this helps :D

Sphen0019265a61a00]<?php
$phone_num = preg_replace(" ", "", $input);
?>

Hope this helps :D

Sphen001pe this helps :D

Sphen001e preg_replace?

Code: Select all

<?php
$phone_num = preg_replace(" ", "", $input);
?>
Hope this helps :D

Sphen001s :D

Sphen001t try a simple preg_replace?

Code: Select all

<?php
$phone_num = preg_replace(" ", "", $input);
?>
Hope this helps :D

Sphen001

Code: Select all

<?php
$phone_num = preg_replace(" ", "", $input);
?>
Hope this helps :D

Sphen001]

Hope this helps :D

Sphen001?

Code: Select all

<?php
$phone_num = preg_replace(" ", "", $input);
?>
Hope this helps :D

Sphen001 helps :D

Sphen001place(" ", "", $input);
?>

Hope this helps :D

Sphen001preg_replace?

Code: Select all

<?php
$phone_num = preg_replace(" ", "", $input);
?>
Hope this helps :D

Sphen001t try a simple preg_replace?

Code: Select all

<?php
$phone_num = preg_replace(" ", "", $input);
?>
Hope this helps :D

Sphen001his helps :D

Sphen001<?php
$phone_num = preg_replace(" ", "", $input);
?>

Hope this helps :D

Sphen001

Posted: Fri Jul 15, 2005 9:26 pm
by Burrito
Sphen001 wrote: Why not just try a simple preg_replace?
this is for JS not php.

you could try just putting in a "real" space next to your escaped whitespace.

Code: Select all

phone = phone.replace(&quote;/ \s*/g&quote;,&quote;&quote;);

Posted: Fri Jul 15, 2005 9:29 pm
by Sphen001
Ahh...forgot to completely read the question again...thanks Burr. :D

Posted: Fri Jul 15, 2005 9:43 pm
by Burrito
well I tried a few different things for you and I'm not certain why what you had doens't work. What I posted doesn't work because of the stupid quotes I put in there...but this works just fine 8O

Code: Select all

<!DOCTYPE HTML PUBLIC &quote;-//W3C//DTD HTML 4.01 Transitional//EN&quote;>

<html>
<head>
	<title>Untitled</title>
<script>
function checkIt(){
	bobs = document.getElementById(&quote;bob&quote;).value;
	alert(bobs);
	bobs = bobs.replace(/\s*/,&quote;&quote;);
	alert(bobs);
	
}
</script>
</head>

<body>
<input type=&quote;text&quote; name=&quote;bob&quote; id=&quote;bob&quote; value=&quote;     hello&quote;>
<input type=&quote;button&quote; onclick=&quote;checkIt()&quote;>


</body>

Posted: Sun Jul 17, 2005 4:38 pm
by raghavan20
The code you sent only replaces the leading spaces

ex:
input : 97097
output:97097

but I wanted to do something like
input : 9709709 9700907
output:97097099700907

I want to find out multiple occurences of /s and replace it with ''

Posted: Sun Jul 17, 2005 5:12 pm
by Chris Corbyn
You had it right the first time, just remove the [ ] and the ^ (the ^ is making it look for spaces right after the start of the string) ;)

You need the "g" modifier with JS to do a "global" search and replace ;)

Posted: Sun Jul 17, 2005 5:12 pm
by Chris Corbyn
Moved to regex

EDIT: To clarify

Code: Select all

/\s+/g

Posted: Mon May 07, 2007 1:42 am
by smudge-it

Posted: Mon May 07, 2007 1:44 am
by John Cartwright
This thread is 2 years old.. thread necromancy generally doesn't fly around here.

Locked.