473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

global string replace w/o regex?

How do you do a global replace in a string without using a regex?

For example,

"ABBA".replace("B", "") returns -"ABA" // I'd like this to be a
global replace
"ABBA".replace(/B/g, "") returns -"AA" // this works correctly but
requires a regex

The reason I ask is that I'd like to have a function that just takes a
single character as an argument and then passes that character to
replace. So the "B" in my example would be a param/variable.

If I have to use a regex is there a way to parameterize it?

var b = "B";
"ABBA".replace(/b/g, "")

Does not work.

-Thx

Jun 18 '07 #1
6 7124
ki*******@gmail.com wrote:
The reason I ask is that I'd like to have a function that just takes a
single character as an argument and then passes that character to
replace. So the "B" in my example would be a param/variable.

If I have to use a regex is there a way to parameterize it?

var b = "B";
"ABBA".replace(/b/g, "")
Use the RegExp constructor:

var b = "B";
"ABBA".replace(new RegExp(b, "g"), "")

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 18 '07 #2
On Jun 18, 7:42 am, kilik3...@gmail.com wrote:
How do you do a global replace in a string without using a regex?
function replace(str,rep,rWith){
return str.replace(new RegExp(rep.replace(/([\^\$\\\.\+\*\?\{\}\(\)\|
\[\]])/g,'\\$1'),"g"),rWith)
}
alert(replace("sdsd.dasd",".","-"))

Jun 18 '07 #3
dd
On Jun 18, 3:46 pm, Martin Honnen <mahotr...@yahoo.dewrote:
Use the RegExp constructor:

var b = "B";
"ABBA".replace(new RegExp(b, "g"), "")
Martin Honnen
The OP could also make a new String prototype method
which I've called rep here:

String.prototype.rep=function(b){
return this.replace(new RegExp(b,"g"),"");
}

and why not this one too (replace with):

String.prototype.repw=function(b,w){
return this.replace(new RegExp(b,"g"),w);
}

Jun 19 '07 #4
dd wrote:
On Jun 18, 3:46 pm, Martin Honnen <mahotr...@yahoo.dewrote:
>Use the RegExp constructor:

var b = "B";
"ABBA".replace(new RegExp(b, "g"), "")
Martin Honnen

The OP could also make a new String prototype method
which I've called rep here:

String.prototype.rep=function(b){
return this.replace(new RegExp(b,"g"),"");
}

and why not this one too (replace with):

String.prototype.repw=function(b,w){
return this.replace(new RegExp(b,"g"),w);
}
Or better still:

String.prototype.repw=function(b,w){
var w = w || '';
return this.replace(new RegExp(b,"g"),w);
}

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 19 '07 #5
-Lost wrote:

>
Or better still:

String.prototype.repw=function(b,w){
var w = w || '';
return this.replace(new RegExp(b,"g"),w);
}
Or:
String.prototype.repw=function(b,w){
return this.replace(new RegExp(b,"g"),w||'');
}

Mick
Jun 19 '07 #6
Michael White wrote:
-Lost wrote:

>>
Or better still:

String.prototype.repw=function(b,w){
var w = w || '';
return this.replace(new RegExp(b,"g"),w);
}

Or:
String.prototype.repw=function(b,w){
return this.replace(new RegExp(b,"g"),w||'');
}
Or... nawww, I got nothing. ;)

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 19 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: ilona | last post by:
Hi all, I store phone numbers in the database as 123447775665554(input mask is used for input, and some numbers have extensions), and I also know from db if the number is Canadian, US, or some...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
2
by: Dan Schumm | last post by:
I'm relatively new to regular expressions and was looking for some help on a problem that I need to solve. Basically, given an HTML string, I need to highlight certain words within the text of the...
4
by: Derek Martin | last post by:
I have an object with several string elements that I would like to check for invalid characters in the properties of each element. Can I use string.replace to do that or is there a better...
5
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able...
16
by: Charles Law | last post by:
I have a string similar to the following: " MyString 40 "Hello world" all " It contains white space that may be spaces or tabs, or a combination, and I want to produce an array...
8
by: Paul | last post by:
Hi, My VB is very rusty I'm afraid! What would be the most efficient way to remove the following ASCII characters from a string? à è ì ò ù À È Ì Ò Ù á é í ó ú ý Á É Í Ó Ú Ý â ê î ô û Â Ê Î Ô...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
3
by: tshad | last post by:
I need to replace a value in a string that could be any case: CREATE Create create And I don't want to change the case of the whole string. Just find the work "Create" and change it to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.