-2

I have this string:

 var str = "jquery12325365345423545423im-a-very-good-string";

What I would like to do, is removing the part 'jquery12325365345423545423' from the above string.

The output should be:

 var str = 'im-a-very-good-string';

How can I remove that part of the string using php? Are there any functions in php to remove a specified part of a string?

sorry for not including the part i have done

I am looking for solution in js or jquery

so far i have tried

var str="jquery12325365345423545423im-a-very-good-string";
str=str.replace("jquery12325365345423545423","");

but problem is numbers are randomly generated and changed every time.

so is there other ways to solve this using jquery or JS

Hitesh
  • 4,098
  • 11
  • 44
  • 82

4 Answers4

4

The simplest solution is to do it with:

str = str.replace(/jquery\d+/, '').replace(' ', '');
hsz
  • 148,279
  • 62
  • 259
  • 315
2

You can use string replace.

var str = "jquery12325365345423545423im-a-very-good-string";
str.replace('jquery12325365345423545423','');

Then to removespaces you can add this.

str.replace(' ','');
R P
  • 33
  • 1
  • 6
1

I think it will be best to describe the methods usually used with this kind of problems and let you decide what to use (how the string changes is rather unclear).

METHOD 1: Regular expression

You can search for a regular expression and replace the part of the string that matches the regular expression. This can be achieved through the JavaScript Replace() method.

In your case you could use following Regular expression: /jquery\d+/g (all strings that begin with jquery and continue with numbers, f.e. jquery12325365345423545423 or jquery0)

As code:

var str="jquery12325365345423545423im-a-very-good-string";
str=str.replace("/jquery\d+/g","");

See the jsFiddle example

METHOD 2: Substring

If your code will always have the same length and be at the same position, you should probably be using the JavaScript substring() method.

As code:

var str="jquery12325365345423545423im-a-very-good-string";
var code = str.substring(0,26);
str=str.substring(26);

See the jsFiddle example

Daniel
  • 20,420
  • 10
  • 92
  • 149
  • Thanks i would go with REGEX ver of answer !! it is working like a charm :) – Hitesh Sep 12 '13 at 05:46
  • http://stackoverflow.com/questions/1789945/how-can-i-check-if-one-string-contains-another-substring-in-javascript – Hitesh Sep 13 '13 at 11:39
0

Run this sample in chrome dev tools

var str="jquery12325365345423545423im-a-very-good-string";
str=str.replace("jquery12325365345423545423","");
console.log(str)
Ankit_Shah55
  • 799
  • 2
  • 9
  • 17