0

I have following string say:

var str = "This is 'first text' and now this is 'second', and many more contents goes here.";

Now I want to replace text between first two quotes first text with some text and next other two quotes with some text like second with some more text

The final string should be something like:

This is 'some text' and now this is 'some more text', and many more contents goes here.

Actually I have to make these two replaces on text change event of two textbox. Up till now, I can replace with specific text only, and not with above conditions.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
Md. Parvez Alam
  • 4,326
  • 5
  • 48
  • 108

1 Answers1

1

If you want to replace the content based on a text match then try

var str = "This is 'fisrt text' and now this is 'second', and many more contents goes here.";

var map = {
    "'fisrt text'": "'some text'",
    "'second'": "'more text'"
}

var str2 = str.replace(/'.*?'/g, function (str) {
    return map[str] || ''
});
console.log(str2)

Demo: Fiddle


If you want to replace it with position(index)

var str = "This is 'fisrt text' and now this is 'second', and many more contents goes here.";

var array = ["'some text'",
    "'more text'"],
    i = 0;

var str2 = str.replace(/'.*?'/g, function (str) {
    return array[i++] || ''
});
console.log(str2)

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • It is strange to choose to remove quoted content as default behavior. Why haven't you choose to do nothing? (i.e. `return map[str] || str;`) – Casimir et Hippolyte Mar 19 '14 at 11:48
  • @Arun , is it not possible some way to find the position of first two quotes and then replace text between them, similar for another two quotes – Md. Parvez Alam Mar 19 '14 at 11:55
  • @Md.ParvezAlam ignore previous one... in that case why can't you use the second version like - http://jsfiddle.net/arunpjohny/3S3mP/2/ – Arun P Johny Mar 19 '14 at 12:01
  • @ArunPJohny can u please put alert rathter than console, please – Md. Parvez Alam Mar 19 '14 at 12:02
  • both text will be replace one by one, so i just want to in a function to replace text between first two quotes with a text, and then another function to replace text between 3 and 4th quotes with some text, sorry @ArunPJohny for being so demanding – Md. Parvez Alam Mar 19 '14 at 12:05
  • @Md.ParvezAlam why is it – Arun P Johny Mar 19 '14 at 12:06
  • i have to replace only one text at a time, – Md. Parvez Alam Mar 19 '14 at 12:07
  • @Md.ParvezAlam can you give a sample text and desired output with 4 quotes – Arun P Johny Mar 19 '14 at 12:07
  • actually there will be four quotes in a text, but on change of first textbox it will replace text between first two quotes with text of textbox, and similar way on change of second textbox it will replace text between 3rd and 4th quotes with second textbox text. – Md. Parvez Alam Mar 19 '14 at 12:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/50027/discussion-between-md-parvez-alam-and-arun-p-johny) – Md. Parvez Alam Mar 19 '14 at 12:14