b3ta.com board
You are not logged in. Login or Signup
Home » Messageboard » XXX » Message 8302289 (Thread)

# PHP and Javascript
Do a whole string replace (or several)
(, Sun 20 Apr 2008, 5:51, archived)
# or replace "." with ". "
then replace " "(two spaces) with " "(one)
(, Sun 20 Apr 2008, 5:52, archived)
# That'd work
unless of course you want to be able to have double spaces elsewhere, then you're fucked
(, Sun 20 Apr 2008, 5:54, archived)
# Exactly.
I need to be able to leave the text pretty much untouched, because that will matter to the people entering it. But I can't allow single periods in the middle of lines of text, it's too risky in terms of form security in the context it will be used.
(, Sun 20 Apr 2008, 5:58, archived)
# Anyone who uses double spaces deserves everything that happens to them -
Tried posting multiple spaces here?
(, Sun 20 Apr 2008, 6:00, archived)
# Er
Y                 es
(, Sun 20 Apr 2008, 6:19, archived)
# Just me then
THERE MUST BE MORE TO LIFE.
(, Sun 20 Apr 2008, 6:25, archived)
# That will put a space between each period in an elipse.
So ... would become . . .
Also, I don't want to run replace twice on the string when I can (hopefully) write a regex that will do the job in one pass. I know it's possible, I just haven't been able to get it right.
(, Sun 20 Apr 2008, 5:55, archived)
# in emergency then replace ". ." with ".."
repeat a few times
(, Sun 20 Apr 2008, 6:03, archived)
# I think the point is he knows he could do this
but is trying to just figure an expression that will handle it in a single pass rather than multiple
(, Sun 20 Apr 2008, 6:04, archived)
# Yeah
I'm using them already on the string previous to this, but I need the very specific replacement routine I've outlined above.
(, Sun 20 Apr 2008, 5:53, archived)
# surely something along the lines of
find .
read next char
if not space
replace with space + char

or

find .
read next char
if not space
insert space in string at relevant point (the position of which you should be able to drag from your loopish searchy algodoodah)
(, Sun 20 Apr 2008, 5:57, archived)
# Yeah, precisely.
And the most efficient way to do that is with JavaScript's replace function and a smart regular expression.
I got very close, I guess I'll just have to persevere until I get the last part right.
(, Sun 20 Apr 2008, 6:00, archived)
# Unless this gets figured out it will keep bothering me now.
Damn my being out of practice. Any chance you could post what you have, I might be able to adapt it a little
(, Sun 20 Apr 2008, 6:02, archived)
# Wait, I'll look back and find the best routine I had.
(, Sun 20 Apr 2008, 6:03, archived)
# Ok
newString = oldString.replace(/\.+[^\.\s]/g, ". ");
Here's the result of this:

"Yes no.Yes no. Yes no. Yes no..."
becomes
"Yes no. es no. Yes no. Yes no..."

It's doing everything I need (which is to say, it's not adding spaces where it shouldn't), with the exception that it replaces the Y after the period.
(, Sun 20 Apr 2008, 6:11, archived)
# That's a bit odd
*thinking*
(, Sun 20 Apr 2008, 6:23, archived)
# Just because it's been so long since i doned anything like this i'm interested to see if I'm right, here it is again
newString = oldString.replace(/\.(?!\.|\s)/g, ". ");
(, Sun 20 Apr 2008, 6:46, archived)
# Oooh you beauty :)
Fucking yeah, that's perfect.
I'll study what you've done and see if I can figure out how it works. I may be able to make some of the earlier string operations more efficient now too.
Thanks man, I owe you one.

Edit: lookahead! Who knew?
(, Sun 20 Apr 2008, 6:56, archived)
# Here's the link i ultimately ended up at:
www.regular-expressions.info/lookaround.html#lookahead

Keep in mind that javascript doesnt support Lookbehind though, only Lookahead.
(, Sun 20 Apr 2008, 6:59, archived)
# Have you tried turning it off and then on again?
/coat

(, Sun 20 Apr 2008, 6:04, archived)
# Morning, sirree!
Up early. Is your sleeping not as well as it should be?
(, Sun 20 Apr 2008, 6:20, archived)
# I got six hours, mustn't grumble
mustn't grumble\\0/

(, Sun 20 Apr 2008, 6:30, archived)
# I'm glad.
You're not down for the London bash are you?
(, Sun 20 Apr 2008, 6:34, archived)
# no, 'fraid not
'fraid not
(, Sun 20 Apr 2008, 7:06, archived)
# Haha
I didn't see you hiding here :)
How are you sir?
(, Sun 20 Apr 2008, 6:21, archived)
# Braw!, thanks!
thanks!you well apart from all this jibber jabber?

(, Sun 20 Apr 2008, 6:30, archived)
# I'm great thanks!)
All this jibber jabber is actually fun to be honest, as long as it gets sorted in the end :)
(, Sun 20 Apr 2008, 6:38, archived)
# Hard way
split the string on "." (there is a Jscript command to do this - can't remember it tho) and check the left char of string 2, then re-assemble string (sucker) Re-iterate for other instances if any.

Sure you'll get away with previous tho!

God I'm such a nerd!

Good luck.
(, Sun 20 Apr 2008, 5:59, archived)
# Concatination is the least efficient string method.
The sequence you've described here would just be too inefficient in the context.
(, Sun 20 Apr 2008, 6:03, archived)
# 'aint life a bitch!
I tried... was never a perfectionist tho.
(, Sun 20 Apr 2008, 6:11, archived)
# I appreciate it :)
(, Sun 20 Apr 2008, 6:21, archived)
# you should ask sprinkles. thats what i do.
he's on msn. but. if he is awake or not...
(, Sun 20 Apr 2008, 6:14, archived)