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
501
votes
27 answers

How to trim a file extension from a String in JavaScript?

For example, assuming that x = filename.jpg, I want to get filename, where filename could be any file name (Let's assume the file name only contains [a-zA-Z0-9-_] to simplify.). I saw x.substring(0, x.indexOf('.jpg')) on DZone Snippets, but wouldn't…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
498
votes
7 answers

How to input a regex in string.replace?

I need some help on declaring a regex. My inputs are like the following: this is a paragraph with<[1> in between and then there are cases ... where the<[99> number ranges from 1-100. and there are many other lines in the txt…
alvas
  • 115,346
  • 109
  • 446
  • 738
493
votes
6 answers

Regular expression search replace in Sublime Text 2

I'm looking to do search replace with regular expressions in Sublime Text 2. The documentation on this is rather anemic. Specifically, I want to do a replace on groups, so something like converting this text: Hello my name is bob And this search…
hackerhasid
  • 11,699
  • 10
  • 42
  • 60
482
votes
17 answers

How to replace all dots in a string using JavaScript

I want to replace all the occurrences of a dot(.) in a JavaScript string For example, I have: var mystring = 'okay.this.is.a.string'; I want to get: okay this is a string. So far I tried: mystring.replace(/./g,' ') but this ends up with all the…
Omar Abid
  • 15,753
  • 28
  • 77
  • 108
470
votes
9 answers

Replace a character at a specific index in a string?

I'm trying to replace a character at a specific index in a string. What I'm doing is: String myName = "domanokz"; myName.charAt(4) = 'x'; This gives an error. Is there any method to do this?
kazinix
  • 28,987
  • 33
  • 107
  • 157
457
votes
28 answers

How to replace multiple substrings of a string?

I would like to use the .replace function to replace multiple strings. I currently have string.replace("condition1", "") but would like to have something like string.replace("condition1", "").replace("condition2", "text") although that does not…
CQM
  • 42,592
  • 75
  • 224
  • 366
456
votes
26 answers

How can I replace text with CSS?

How can I replace text with CSS using a method like this: .pvw-title img[src*="IKON.img"] { visibility:hidden; } Instead of ( img[src*="IKON.img"] ), I need to use something that can replace text instead. I have to use [ ] to get it to…
MokiTa
  • 4,760
  • 3
  • 15
  • 16
427
votes
3 answers

JavaScript - Replace all commas in a string

I have a string with multiple commas, and the string replace method will only change the first one: var mystring = "this,is,a,test" mystring.replace(",","newchar", -1) Result: "thisnewcharis,a,test" The documentation indicates that the default…
mike
  • 22,931
  • 31
  • 77
  • 100
407
votes
11 answers

Finding and replacing elements in a list

I have to search through a list and replace all occurrences of one element with another. So far my attempts in code are getting me nowhere, what is the best way to do this? For example, suppose my list has the following integers a = [1, 2, 3, 4, 5,…
James
  • 4,097
  • 3
  • 16
  • 4
371
votes
9 answers

How to use string.replace() in python 3.x

The string.replace() is deprecated on python 3.x. What is the new way of doing this?
Dewsworld
  • 13,367
  • 23
  • 68
  • 104
362
votes
16 answers

Best way to replace multiple characters in a string?

I need to replace some characters as follows: & ➔ \&, # ➔ \#, ... I coded as follows, but I guess there should be some better way. Any hints? strs = strs.replace('&', '\&') strs = strs.replace('#', '\#') ...
prosseek
  • 182,215
  • 215
  • 566
  • 871
353
votes
7 answers

Replace spaces with dashes and make all letters lower-case

I need to reformat a string using jQuery or vanilla JavaScript Let’s say we have "Sonic Free Games". I want to convert it to "sonic-free-games". So whitespaces should be replaced by dashes and all letters converted to small letters. Any help on this…
EzzDev
  • 9,900
  • 12
  • 35
  • 35
353
votes
10 answers

How to search and replace using grep

I need to recursively search for a specified string within all files and subdirectories within a directory and replace this string with another string. I know that the command to find it might look like this: grep 'string_to_find' -r ./* But how…
billtian
  • 6,589
  • 4
  • 18
  • 28
346
votes
22 answers

How to search and replace text in a file

How do I search and replace text in a file using Python 3? Here is my code: import os import sys import fileinput print("Text to search for:") textToSearch = input("> ") print("Text to replace it with:") textToReplace = input("> ") print("File to…
Shriram
  • 4,711
  • 6
  • 20
  • 22
338
votes
13 answers

How to swap text based on patterns at once with sed?

Suppose I have 'abbc' string and I want to replace: ab -> bc bc -> ab If I try two replaces the result is not what I want: echo 'abbc' | sed 's/ab/bc/g;s/bc/ab/g' abab So what sed command can I use to replace like below? echo abbc | sed…
DaniloNC
  • 3,555
  • 2
  • 16
  • 8