R4mirezZ
R4mirezZ

Reputation: 39

How to remove string from string until a specefic character using jquery

I want to remove the last parameter in the href of a specific tag using jquery

for example replace href="app/controller/action/05/04/2014"

by href="app/controller/action/05/04"

Upvotes: 2

Views: 310

Answers (3)

vivek_nk
vivek_nk

Reputation: 1600

We can use a regular expression replace which will be the fastest, compared to substring/slice.

var hreftxt ="app/controller/action/05/04/2014";
hreftxt = hreftxt.replace(/\/[^\/]*$/,"");

Upvotes: 0

Nezir
Nezir

Reputation: 6925

if you know which value you need to change ,than you can use replace:

var str = "app/controller/action/05/04/2014";
var res = str.replace("2014","04");

or else you can use array and change / update last value in array:

var str = "app/controller/action/05/04/2014";
var res = str.split("/");

Upvotes: 0

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

Try using the String.lastIndexOf() and String.substring() in this context to achieve what you want,

var xText ="app/controller/action/05/04/2014";
xText = xText.substring(0,xText.lastIndexOf('/'));

DEMO

Upvotes: 3

Related Questions