Questions tagged [replace]

Replacing is the action of searching a string for a sub-string and replacing it with a different string.

Replacing is the action of searching a string (the haystack) for a sub-string (the needle) and replacing it with a different string.

for example, replacing all 'l' within 'Hello' with 'y', would result in 'Heyyo'.

27903 questions
146
votes
16 answers

JavaScript: replace last occurrence of text in a string

See my code snippet below: var list = ['one', 'two', 'three', 'four']; var str = 'one two, one three, one four, one'; for ( var i = 0; i < list.length; i++) { if (str.endsWith(list[i]) { str = str.replace(list[i], 'finish') …
Ruth
  • 5,646
  • 12
  • 38
  • 45
145
votes
9 answers

Insert space before capital letters

I have a string "MySites". I want to place a space between My and Sites. How can I do this in jQuery or JavaScript?
CLiown
  • 13,665
  • 48
  • 124
  • 205
145
votes
8 answers

PowerShell Script to Find and Replace for all Files with a Specific Extension

I have several configuration files nested like such: C:\Projects\Project_1\project1.config C:\Projects\Project_2\project2.config In my configuration I need to do a string replace like such: will become:
Brandon
  • 10,744
  • 18
  • 64
  • 97
144
votes
25 answers

How can I perform a str_replace in JavaScript, replacing text in JavaScript?

I want to use str_replace or its similar alternative to replace some text in JavaScript. var text = "this is some sample text that i want to replace"; var new_text = replace_in_javascript("want", "dont want", text); document.write(new_text); should…
Vish
  • 4,508
  • 10
  • 42
  • 74
143
votes
5 answers

How can I replace a regex substring match in Javascript?

var str = 'asd-0.testing'; var regex = /asd-(\d)\.\w+/; str.replace(regex, 1); That replaces the entire string str with 1. I want it to replace the matched substring instead of the whole string. Is this possible in Javascript?
dave
  • 7,717
  • 19
  • 68
  • 100
141
votes
6 answers

How to search and replace globally, starting from the cursor position and wrapping around the end of file, in a single command invocation in Vim?

When I search with the / Normal-mode command: /\vSEARCHTERM Vim starts the search from the cursor position and continues downwards, wrapping around to the top. However, when I search and replace using the :substitute…
basteln
  • 2,483
  • 2
  • 19
  • 20
140
votes
6 answers

Ruby replace string with captured regex pattern

I am having trouble translating this into Ruby. Here is a piece of JavaScript that does exactly what I want to do: function get_code(str){ return str.replace(/^(Z_.*): .*/,"$1")​​​​​​​​​​​​​​​​​​​​​​​​​​​; } I have tried gsub, sub, and replace…
JD Isaacks
  • 56,088
  • 93
  • 276
  • 422
140
votes
12 answers

Find and replace in Visual Studio code in a selection

I have the following line in a file I'm editing in VSCode: ...............111.........111.............111.. I want to replace all .s with 0s. However, when I highlight the line and do a find/replace for .s, all the .s in the document are replaced,…
Adam
  • 8,752
  • 12
  • 54
  • 96
138
votes
19 answers

Replace substring with another substring C++

How could I replace a substring in a string with another substring in C++, what functions could I use? eg: string test = "abc def abc def"; test.replace("abc", "hij").replace("def", "klm"); //replace occurrence of abc and def with other substring
Steveng
  • 2,385
  • 6
  • 23
  • 20
136
votes
9 answers

How to delete and replace last line in the terminal using bash?

I want to implement a progress bar showing elapsed seconds in bash. For this, I need to erase the last line shown on the screen (command "clear" erases all the screen, but I need to erase only the line of the progress bar and replace it with the new…
Debugger
  • 9,130
  • 17
  • 45
  • 53
136
votes
5 answers

Eclipse, regular expression search and replace

In eclipse, is it possible to use the matched search string as part of the replace string when performing a regular expression search and replace? Basically, I want to replace all occurrences…
user21037
135
votes
13 answers

Search and replace in Vim across all the project files

I'm looking for the best way to do search-and-replace (with confirmation) across all project files in Vim. By "project files" I mean files in the current directory, some of which do not have to be open. One way to do this could be to simply open all…
psyho
  • 7,142
  • 5
  • 23
  • 24
132
votes
9 answers

How to replace negative numbers in Pandas Data Frame by zero

I would like to know if there is someway of replacing all DataFrame negative numbers by zeros?
Hangon
  • 2,449
  • 7
  • 23
  • 31
131
votes
7 answers

Can I replace groups in Java regex?

I have this code, and I want to know, if I can replace only groups (not all pattern) in Java regex. Code: //... Pattern p = Pattern.compile("(\\d).*(\\d)"); String input = "6 example input 4"; Matcher m = p.matcher(input); if…
wokena
  • 1,301
  • 4
  • 15
  • 17
129
votes
6 answers

How to replace multiple strings in a file using PowerShell

I am writing a script for customising a configuration file. I want to replace multiple instances of strings within this file, and I tried using PowerShell to do the job. It works fine for a single replace, but doing multiple replaces is very slow…
Ivo Bosticky
  • 6,338
  • 6
  • 34
  • 35