views:

54

answers:

4

I am having trouble getting a regex to work. This is the string.

"some text would be here and Blah St:39.74908:-104.99482:272~Turn right over here"

I need to remove the

:39.74908:-104.99482:272~

part of the string. I am using jQuery if that helps.

+3  A: 
var your_string = "some text would be here and Blah St:39.74908:-104.99482:272~Turn right over here";
alert(your_string.replace(/:.+~/, "")); /* some text would be here and Blah StTurn right over here */
chigley
That works great, thanks chigley
jhanifen
You might consider making the ".+" lazy - `string.replace(/:.+?~/, "")`. That way, if there's ever more text in the string, possibly containing a tilde, it won't be consumed.
Connor M
A: 
var string = 'some text would be here and Blah St:39.74908:-104.99482:272~Turn right over here'
var string2 = string.replace(/:-?\d+(?:\.\d+)?:-?\d+(?:\.\d+)?:-?\d+(?:\.\d+)?~/g, '')

Will replace all instances of :number:number:number~

The numbers can be negative and can have decimals

cjavapro
+4  A: 
var str = "some text would be here and Blah St:39.74908:-104.99482:272~Turn right over here";
alert(str.replace(/:[^~]+~/g, ""));
Tim Down
I'd use this one. Would seem to be a little safer.
patrick dw
A: 

You don't need an extremely complex regex for this:

var str = 'Blah St:39.74908:-104.99482:272~Turn right over here';

str.replace(/:.*~/, '');
Sander Rijken