2010-10-01 4 views
3

J'ai de la difficulté à faire fonctionner une regex. C'est la chaîne.RegEx Question - Tout supprimer entre: et ~

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

Je dois enlever le

:39.74908:-104.99482:272~ 

partie de la chaîne. J'utilise jQuery si cela aide.

Répondre

3
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 */ 
+0

Cela fonctionne très bien, merci chigley – jhanifen

+2

Vous pourriez envisager de rendre le ". +" Paresseux - 'string.replace (/:.+? ~ /," ")'. De cette façon, s'il y a toujours plus de texte dans la chaîne, contenant éventuellement un tilde, il ne sera pas consommé. –

0
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+)?){3}~/g, '') 

remplacera toutes les instances de: Numéro: Numéro: Numéro ~

Les chiffres peuvent être négatifs et peut avoir des décimales

4
var str = "some text would be here and Blah St:39.74908:-104.99482:272~Turn right over here"; 
alert(str.replace(/:[^~]+~/g, "")); 
+0

J'utiliserais celui-ci. Cela semblerait être un peu plus sûr. – user113716

0

Vous ne pas besoin d'un extrêmement complexe regex pour cela:

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

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