0

I'm trying to create a Find/Replace thing for my text area. I got the find done but I can't get the replace code together.

My search code is:

function setSelectionRange(input, selectionStart, selectionEnd) {
    input.focus();
    input.setSelectionRange(selectionStart, selectionEnd);
}

function selectString(input, string) {
    var match = new RegExp(string, "i").exec(input.value);
    if (match) {
        setSelectionRange(input, match.index, match.index + match[0].length);
    }
}

How would I make a replace function if possible?

zdrsh
  • 1,667
  • 1
  • 14
  • 26
Shawn31313
  • 5,978
  • 4
  • 38
  • 80

2 Answers2

0

For the find dialog:

I had the same question and nobody could answer it! I made a JDialog with a JLabel, JTextField and two JButtons (next and previous). I added action listeners so that when a key was released on the JTextField it would select the first occurrence of the word using .indexOf(myTextField.getText());. I then had the next and previous buttons find the word with action listeners. If you still want the code then just comment, I'll find it on my computer.

Scott
  • 3
  • 2
0

You'll want to use the replace function in Javascript. It accepts a regular expression for the search string.

string = string.replace(input.value, "replace value here")
endyourif
  • 2,186
  • 19
  • 33