How to proper insert variable in upper code instead of fixed string 'something' ?
__________________
Found a flower or bug and don't know what it is ? agrozoo.net galery
if you don't spot search button at once, there is search form: agrozoo.net galery search
One thing I did notice, however, is that if you're trying to replace more than just one instance of something, you can split on something, then join on "".
s.split(something);
s.join("");
This will replace all instances of variable something with "". If you have two or more spaces in the resulting string, then use regex to replace all double-or-more instances of space with just single space.
__________________ ^_^
If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
* The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
Use the constructor RegExp with something like this
Code:
var something = "foobar";
var rgx = new RegExp(something,"gi");
You need to escape the 11 following characters (often called "metacharacters") with a backslash :
the opening square bracket [,
the backslash \,
the caret ^,
the dollar sign $,
the period or dot .,
the vertical bar or pipe symbol |,
the question mark ?,
the asterisk or star *,
the plus sign +,
the opening round bracket (
and the closing round bracket ).
Then the script is a little more complicated (if the string contains metacharacters)
Code:
<script type="text/javascript">
var something="3+5=4";
var strForRegExp=something.replace(/(\[|\\|\^|\$|\.|\||\?|\*|\+|\(|\))/g,"\\$1");
// alert(strForRegExp);
var rgx=new RegExp(strForRegExp,'g');
var rsp=" foo 3+5=4 bar".replace(rgx,'');
alert(rsp)
</script>
Last edited by 007julien; 12-07-2012 at 02:17 PM..
<html>
<head>
<script type="text/javascript">
// REPLACE VARIABLE VALUE IN TEXTBOX OR TEXTAREA OR DIV BY ANOTHER VARIABLE VALUE
function doRep(IDS, val2replace, change2, elem) {
if (elem == "T") {
var txt = document.getElementById(IDS).value; // string to be analysed
}
if (elem == "D") {
var txt = document.getElementById(IDS).innerHTML; // string to be analyzed
}
var match = new RegExp(val2replace, 'gi'); // setting regex with val2replace global and case insensitive
var output = txt.replace(match, change2); // replacing variable string with variable replacement value
if (elem == "T") {
document.getElementById(IDS).value=output;
}
if (elem == "D") {
document.getElementById(IDS).innerHTML=output;
}
}
</script>
</head>
<body>
<input type = "text" id = "txt1" value = "Now is the time for all good men to come to the aid of their country." size = "70">
<br><br>
<textarea id="txt2" cols="40" rows="5">
Now is the time for all good men
to come to the aid of their country.
</textarea>
<br><br>
<div id="divText">
Now is the time for all good men<br>
to come to the aid of their country.
</div>
<br>
Find: <input type="text" id="txt2find" value=""><br>
Replace: <input type="text" id="reptext" value=""><br>
<br><br>
<button
onclick="doRep('txt1',document.getElementById('txt2find').value,document.getElementById('reptext').value, 'T')">
Textbox: Do it</button>
<button
onclick="doRep('txt2',document.getElementById('txt2find').value,document.getElementById('reptext').value, 'T')">
Textarea: Do it</button>
<button
onclick="doRep('divText',document.getElementById('txt2find').value,document.getElementById('reptext').value, 'D')">
DIV: Do it</button>
</body>
</html>
The 11 meta-characters mentioned by julien007 must be escaped in the "to find" string but not of course in the "replace by" string.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Last edited by Philip M; 12-07-2012 at 04:30 PM..
Reason: Improved
One thing I did notice, however, is that if you're trying to replace more than just one instance of something, you can split on something, then join on "".
s.split(something);
s.join("");
This will replace all instances of variable something with "". If you have two or more spaces in the resulting string, then use regex to replace all double-or-more instances of space with just single space.
How is that different from / better than
Code:
var txt = "Here is something and something and something";
txt = txt.replace(/(\s)?something/gi,"");
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
After a few tests the \ should be treated separately.
The following method may be helpful
Code:
<script type="text/javascript">
String.prototype.toRegExp=function(){return this.replace(/\\/g,'\\\\').replace(/(\[|\^|\$|\.|\||\?|\*|\+|\(|\))/g,'\\$1')}
//Example of use with all the «meta»characters
var something="[\\^$.|?*+()";// the \ is to escape in this string.
alert(("foo"+something+"bar").replace(new RegExp(something.toRegExp(),'g'),'')); //=>"foobar"
After a few tests the \ should be treated separately.
The following method may be helpful
Code:
<script type="text/javascript">
String.prototype.toRegExp=function(){return this.replace(/\\/g,'\\\\').replace(/(\[|\^|\$|\.|\||\?|\*|\+|\(|\))/g,'\\$1')}
//Example of use with all the «meta»characters
var something="[\\^$.|?*+()";// the \ is to escape in this string.
alert(("foo"+something+"bar").replace(new RegExp(something.toRegExp(),'g'),'')); //=>"foobar"
Here is one way to allow variables in both the source regexp string and also the replacement string - using the evil eval .
Code:
alert(
// String replace with variable source regexp and variable replacement string via eval.
"visible global variable values:"+
(var1 = " simple") + " " +
(var2 = "n evaled") + " " +
(var3=[" little"," small", " tiny"]) + " " +
(i=0) + " " + (cnt=0)
+ // (1)
"\n\nThis is a simple example with a simple string" +
"\nThis is a simple example with a simple string".replace(eval("/"+var1+"/gi"),var2)+" "
+ // -OR- (2)
"\n\nThis is a simple example with a very simple really simple simple simple and I mean simple string"+
"\nThis is a simple example with a very simple really simple simple simple and I mean simple string".replace(eval("/"+var1+"/gi"),
function(n){
return i++%2?var3[cnt++]:n; //replace every other value
})
+ // -OR- (3)
"\n\nThis is a simple example with a simple string"+
"\nThis is a simple example with a simple string".replace(eval("/"+var1+"/gi"),
(function(m){
var i=0;
var a=function(n){
return m[i++];
};
return function(n){return a(n)}
})(var3))
);