I know that str.replace(/x/g, "y")replaces all x's in the string but I want to do this  function name(str,replaceWhat,replaceTo){     str.replace(/replaceWhat/g,replaceTo); }   How can i use a variable in the first argument?
6461
I would like to create a String.replaceAll() method in JavaScript and I'm thinking that using a regex would be most terse way to do it.  However, I can't figure out how to pass a variable in to a regex.  I can do this already which will replace all the instances of "B" with "A".  "ABABAB".replace(/B/g, "A");   But I want to do something like this:  String.prototype.replaceAll = function(replaceThis, withThis) {     this.replace(/replaceThis/g, withThis); };   But obviously this will only replace the text "replaceThis"...so how do I pass this variable in to my regex string?
106661
I have an input file, say, file1, which appears as follows:  WRITE=2 START=1 SMEAR=0 MIN=6 MAX=100   My problem is to search for the string SMEAR and if the string exists replace the entire line with SMEAR=-5. However, if the SMEAR does not exist, then I need to insert a new line SMEAR=-5. Ultimately the line SMEAR=-5 should appear only once. I can replace the string using sed:   sed -i '/SMEAR/c\SMEAR=-5' file1   Or I can insert the string as a new line    sed -i '10i SMEAR=-5' file1   But how can I combine them based on the existence of the string in the file?  EDIT: Also it would be helpful if I can pass the value of SMEAR as a variable.
24985
I saw , but I didn't see a JavaScript specific example. Is there a simple string.Empty available in JavaScript, or is it just a case of checking for ""?
40918
I have a string coming from a table like "can no pay{1},as your payment{2}due on {3}". I want to replace {1} with some value , {2} with some value and {3} with some value .  Is it Possible to replace all 3 in one replace function ? or is there any way I can directly write query and get replaced value ?   I want to replace these strings in Oracle stored procedure the original string is coming from one of my table I am just doing select on that table   and then I want to replace {1},{2},{3} values from that string to the other value that I have from another table
60984
If you want to use global functions and variable dynamically you can use:  window[functionName](window[varName]);   Is it possible to do the same thing for variables in the local scope?  This code works correctly but currently uses eval and I'm trying to think of how else to do it.  var test = function(){     //this = window     var a, b, c; //private variables      var prop = function(name, def){         //this = window         eval(name+ ' = ' + (def.toSource() || undefined) + ';');              return function(value){             //this = test object             if ( !value) {                 return eval('(' + name + ')');             }             eval(name + ' = value;')             return this;         };      };      return {         a:prop('a', 1),         b:prop('b', 2),         c:prop('c', 3),         d:function(){             //to show that they are accessible via to methods             return [a,b,c];         }     }; }();  &gt;&gt;&gt;test Object &gt;&gt;&gt;test.prop undefined &gt;&gt;&gt;test.a function() &gt;&gt;&gt;test.a() 1 //returns the default &gt;&gt;&gt;test.a(123) Object //returns the object &gt;&gt;&gt;test.a() 123 //returns the changed private variable &gt;&gt;&gt;test.d() [123,2,3]
17800
I have a file with multiple instances of Text_1 and Text1 and I need to replace both those strings with Text_A and TextB respectively.    Currently I'm doing two Find and Replace functions on each file one that finds Text_1 and replaces it with Text_A and the other that finds Text1 and replaces it with TextB.    Is there any way to do this all at once instead of having to run "Find and Replace" twice?  I am using Dreamweaver CS3, but I also have Notepad++, regular Notepad, OO Writer, MS Word if those will be easier.  Ideally I could do this in Dreamweaver or Notepad++ but I'm open to downloading something else to get the job done.  I'd prefer not to have to do any command line stuff or create a batch file (while I'm aware of it, I don't understand it really).  In case the above description isn't clear, let me explain it this way...  I want to run Find &amp; Replace 1 time in 1 document and I want it to do ALL of the following during that one Find &amp; Replace instance:   Find Text_1 and Replace with Text_A Find Text1 and Replace with TextB   I am not trying to do a Find and Replace across several documents.
12917
I would like to use variable substitution on a particular string that I access via a command. For example, if I copy something into my clipboard, I can access it like this.  $ xclip -o -selection clipboard Here's a string I just copied.   If I assign it to a variable, then I can do variable substitution on it.  $ var=$(xclip -o -selection clipboard) $ echo $var Here's a string I just copied. $ echo ${var/copi/knott} Here's a string I just knotted.   However, is there a way to do variable substitution without assigning it to a variable? Conceptually, something like this.  $ echo ${$(xclip -o -selection clipboard)/copi/knott} bash: ${$(xclip -o -selection clipboard)/copi/knott}: bad substitution   This syntax fails, because var should be a variable name, not a string.
2158
This works as expected:  $ echo a b c | xargs --replace="{}" echo x "{}" y x a b c y   This does too:  $ echo a b c | xargs --max-args=1 echo x x a x b x c   But this doesn't work as expected:  $ echo a b c | xargs --max-args=1 --replace="{}" echo x "{}" y x a b c y   And neither does this:  $ echo a b c | xargs --delimiter=' ' --max-args=1 --replace="{}" echo x "{}" y x a y x b y x c  y   I expected this output:  x a y x b y x c y   As a workaround, I am using printf and two xargs, but that is ugly:  $ echo a b c | xargs printf '%s\0' | \ &gt; xargs --null --max-args=1 --replace="{}" echo x "{}" y x a y x b y x c y   Any idea why this is happening?
4791
Say i have this PHP code:  $FooBar = "a string";   i then need a function like this:  print_var_name($FooBar);   which prints:  FooBar   Any Ideas how to achieve this? Is this even possible in PHP?
34527
How can I (easily) take a string such as "sin(x)*x^2" which might be entered by a user at runtime and produce a Python function that could be evaluated for any value of x?
62239
Coming from a C# background the naming convention for variables and method names are usually either camelCase or PascalCase:  // C# example string thisIsMyVariable = "a" public void ThisIsMyMethod()   In Python, I have seen the above but I have also seen underscores being used:  # python example this_is_my_variable = 'a' def this_is_my_function():   Is there a more preferable, definitive coding style for Python?
25338
I need to search a string and replace all occurrences of %FirstName% and %PolicyAmount% with a value pulled from a database. The problem is the capitalization of FirstName varies. That prevents me from using the String.Replace() method. I've seen web pages on the subject that suggest  Regex.Replace(strInput, strToken, strReplaceWith, RegexOptions.IgnoreCase);   However for some reason when I try and replace %PolicyAmount% with $0, the replacement never takes place. I assume that it has something to do with the dollar sign being a reserved character in regex.   Is there another method I can use that doesn't involve sanitizing the input to deal with regex special characters?
27528
I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing.    Is there a function that can do this easily?  For example:  $string1 = "I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing."; $string2 = 100charfunction($string1); print $string2   To get:  I am looking for a way to pull the first 100 characters from a string vari
98050
As part of a project for school, I need to replace a string from the form:  5 * x^3 - 6 * x^1 + 1   to something like:  5x&lt;sup&gt;3&lt;/sup&gt; - 6x&lt;sup&gt;1&lt;/sup&gt; + 1   I believe this can be done with regular expressions, but I don't know how to do it yet.  Can you lend me a hand?   P.S. The actual assignment is to implement a Polynomial Processing Java application, and I'm using this to pass polynomial.toString() from the model to the view, and I want do display it using html tags in a pretty way.
24071
I'm trying to understand Python's approach to variable scope. In this example, why is f() able to alter the value of x, as perceived within main(), but not the value of n?  def f(n, x):     n = 2     x.append(4)     print('In f():', n, x)  def main():     n = 1     x = [0,1,2,3]     print('Before:', n, x)     f(n, x)     print('After: ', n, x)  main()   Output:  Before: 1 [0, 1, 2, 3] In f(): 2 [0, 1, 2, 3, 4] After:  1 [0, 1, 2, 3, 4]
50285
Is there a way to substring a string in Python, to get a new string from the third character to the end of the string?  Maybe like myString[2:end]?  If leaving the second part means 'till the end', and if you leave the first part, does it start from the start?
4447
I have a string representing a path.  Because this application is used on Windows, OSX and Linux, we've defined environment variables to properly map volumes from the different file systems.  The result is:  "$C/test/testing"   What I want to do is evaluate the environment variables in the string so that they're replaced by their respective volume names.  Is there a specific command I'm missing, or do I have to take os.environ.keys() and manually replace the strings?