Help: regular expression: removing white spaces

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Locked
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Help: regular expression: removing white spaces

Post 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
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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;);
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post by Sphen001 »

Ahh...forgot to completely read the question again...thanks Burr. :D
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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>
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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 ''
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Moved to regex

EDIT: To clarify

Code: Select all

/\s+/g
smudge-it
Forum Newbie
Posts: 2
Joined: Mon May 07, 2007 1:15 am
Location: London, UK

Post by smudge-it »

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

This thread is 2 years old.. thread necromancy generally doesn't fly around here.

Locked.
Locked