• 0

[jQuery] replace part of a string


Question

Right now I have a ajax/jquery script that sends a variable to a script and it replies back with the data I want.

I want to add a character to the string, in front of a specific character.

I want to take a string like this:

"tommy51"

and replace it with:

"tommy 51"

I also want to assign the output data to a value of ID "name".

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Since you said, in front of a specific character, you could do something like this

var mystring = "tommy51";
mystring = mystring.replace('51', ' 51');
alert(mystring);

Regex would be your best choice :) but the above will work well.

Link to comment
Share on other sites

  • 0

The solution depends on a few things. Let's say you want to take string A and insert character B before character C

- Will B always be the same character? i.e., will you always be inserting the same character into your strings?

- How will you determine C? Is C always the same character? What if that character appears more than once in the string?

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.