Mateo
Mateo

Reputation: 177

Replacing text using regular expressions in JavaScript

I want to replace a simple text like: 1 day ago to Hace 1 dia

I have tried the following code, but it does not work:

var texto = "1 day ago";
texto = texto.replace('/\d+(?=day ago)/', "Hace $1 dia");

Upvotes: 2

Views: 132

Answers (3)

Madara's Ghost
Madara's Ghost

Reputation: 174947

var texto = "1 day ago";
texto = texto.replace(/(\d+) day(s?) ago/i, "Hace $1 dia$2");

I've expanded it a little to allow for "N days ago" as well.

Upvotes: 4

ace
ace

Reputation: 7583

Should be

texto = texto.replace(/(\d+) days? ago/, "Hace $1 dia");

Upvotes: 2

Thomas Clayson
Thomas Clayson

Reputation: 29925

should be.......

/(\d+) day ago/i

Upvotes: 0

Related Questions