-1

Here's what I'm trying to do. Honestly, I'm not even sure where to start.

var text1 = 'ThE foX iS brown',

        text2 = 'the fox is brown the fox is brown THE FOX IS BROWN',

        index = text2.toLowerCase().indexOf(text1.toLowerCase()),

        output = 'do something with substring?';

    alert(index);

The "text2" should be changed to: ThE foX iS brown the fox is brown THE FOX IS BROWN

    var text1 = 'ThE foX iS brown the FOX is BrO',

        text2 = 'the fox is brown the fox is brown THE FOX IS BROWN',

        index = text2.toLowerCase().indexOf(text1.toLowerCase()),

        output = 'do something with substring?';

    alert(index);

And in this case "text2" should be changed to: ThE foX iS brown the FOX is BrOwn THE FOX IS BROWN

I thought that maybe I should first try to find the "first" occurrence of the string "text1" within "text2" but it doesn't seem to work? And I thought after that I could use substring? Or maybe there's even an easier way to do this? I'm not sure. Any help would be really appreciated. Thank you.

isuckatcoding
  • 111
  • 2
  • 7
  • According to downvotes, this seems like a dumb question. Sorry. – isuckatcoding Jun 19 '13 at 20:47
  • it's not dumb, it just doesn't show much effort on your part. you mentioned a few ideas you had - why not try some of them then come back when you have a more specific question? – user428517 Jun 19 '13 at 20:51
  • You can't modify strings in place in Javascript. Functions like `toLowerCase()` return a new string. – Barmar Jun 19 '13 at 20:51
  • Can the match for `text1` be somewhere other than the beginning of `text2`? – Barmar Jun 19 '13 at 20:54
  • @Barmar No, should start from beginning. I tried `replace`, but that cut off the rest of the string? And in this code above, it does not return the index, it's only empty. – isuckatcoding Jun 19 '13 at 21:00
  • possible duplicate of [javascript replacing strings](http://stackoverflow.com/questions/7769808/javascript-replacing-strings) – jball Jun 20 '13 at 16:43

2 Answers2

1

It seams like you are simply trying to replace the first part of the string with text1.

You can try this.

int text1Size = text1.length;
output = text1 + text2.substring(text1Size);
Nick Humrich
  • 14,905
  • 8
  • 62
  • 85
1

What you are looking for is to replace the first occurrence of the substring text1 in text2 with text1, ignoring the case.

You can build a regular expression for that, using the "i"gnore-case flag.

Assuming you don't know the contents of text1, you have to escape some special characters interpreted by regular expressions. Once you have your regular expression you can use replace.

Here is an example:

// Function to escape Regular Expressions special characters.
// From: http://stackoverflow.com/questions/3115150/how-to-escape-regular-expression-special-characters-using-javascript
RegExp.escape = function(text) {
  return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};

var text1 = 'ThE foX iS brown';
var text2 = 'the fox is brown the fox is brown THE FOX IS BROWN';

// Create the Regular Expression with the "i"gnore-case flag
var re = new RegExp(RegExp.escape(text1), "i");

var output = text2.replace(re, text1);
// output -> ThE foX iS brown the fox is brown THE FOX IS BROWN

And here is a working fiddle.