document.getElementById("Message").innerHTML=document.getElementById("Message").innerHTML.replace("'","%%");
The above statement replaces only the first occurrence of the single quote. Could it be because that I do a submit right after that, and that javascript doesn't wait for the previous statement to be completed before moving on to the next one?
Try using regex /g
.replace(/'/g,"%%")
Change your code as below,
document.getElementById("Message").innerHTML =
document.getElementById("Message")
.innerHTML
.replace(/'/g,"%%");
To replace globally in javascript you need to add /g to the replacement string.
See this SO link How to replace all dots in a string using JavaScript
You should use regular expression in replace.
document.getElementById("Message").innerHTML=document.getElementById("Message").innerHTML.replace(/'/g,"%%");
jsfiddle
Use the Global (g) flag in the replace() parameters.
See here
You should use a regular expression and the /g (global) flag. This will replace all occurrences:
document.getElementById("Message").innerHTML=
document.getElementById("Message").innerHTML.replace(/'/g,"%%");
http://www.w3schools.com/jsref/jsref_replace.asp
Related
I want to use Regular expression to replace this text :
[Button size="Big" color="#000"] test [/Button]
to Button, I used this site http://www.freeformatter.com/regex-tester.html but not working the replace.
the Regular expression \[Button([^\]]*)\[/Button],it give me the result String is same as before replace! what the mistake?
The ([^\]]*) part of your regex will stop matching before the closing ] of first tag. So, you don't have the pattern to match string - "] test " thereafter.
Modify your regex to:
\[Button([^\]]*][^\[]*)\[/Button]
Try with this, it matches properly and also matches the insides.
\[Button([^\]]*)\](.*?)\[/Button\]
The regex of
\[Button.*\]
and the replacement string of Button should be enough.
I need to replace a string (str1) with another string (str2) every time str1 shows in a specific div.
This is what I got so far
<script type="text/javascript">
$(window).load(function(){
var str=document.getElementById("foo").innerHTML;
var n=str.replace("Google","Yahoo");
document.getElementById("foo").innerHTML=n;
});
</script>
and the html
<div id="foo">
Google is the best website ever <br />
Google is not the best website ever</div>
Unfortunately, when I run it, it only replaces the first instance of the word Google.
What am I doing wrong? What do I need to add to make it replace ALL the instances of the word?
Use regex /string/g to replace all occurrences, you are using substring which will replace only first occurances as per documentation of replace() function.
Live Demo
var n=str.replace(/Google/g,"Yahoo");
String.prototype.replace()
The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match.
str.replace(regexp|substr, newSubStr|function)
You are using substr pattern which will replace first occurance only.
substr (pattern)
A String that is to be replaced by newSubStr. It is treated as a
verbatim string and is not interpreted as a regular expression. Only
the first occurrence will be replaced.
Use this regexp patter to replace all occurances.
regexp (pattern)
A RegExp object or literal. The match is replaced by
the return value of parameter #2.
I am trying to learn javascript
i have this code:
x=x.replace(/^\s+|\s+$/g,"");
can i have a description about /^\s+|\s+$/g,""
searching to replace what with what ?
Regards
This is a regular expression. Basically \s matches whitespac characters and replaces them with "".
edit:
/ ... / marks the regex.
^\s+ Take 1or more whitespaces at the beginning of the string.
\s+$ Take 1 or more whitespaces at the end of the string.
/g Dont stop at the first match, but find all matches - "global flag"
It's removing whitespaces, either at the beginning or the end of the string, by replacing them with the empty string "".
You can find more about regular expressions here http://www.w3schools.com/jsref/jsref_obj_regexp.asp.
first, look at this: http://www.w3schools.com/jsref/jsref_replace.asp and then do not use stackoverflow for questions of this kind
I have the string R.E.M. and I need to make it REM
So far, I have:
$('#request_artist').val().replace(".", "");
...but I get RE.M.
Any ideas?
The first argument to replace() is usually a regular expression.
Use the global modifier:
$('#request_artist').val().replace(/\./g, "");
replace() at MDC
You could pass a regular expression to the replace method and indicate that it should replace all occurrences like this: $('#request_artist').val().replace(/\./g, '');
The method used to replace the string is not recursive, meaning once it found a matching char or string, it stop looking. U should use a regular expression replace. $("#request_artist").val().replace(/\./g, ''); Check out Javascript replace tutorial for more info.
Using JavaScript and I want to replace any text between #anytext# with some text. I want to make it generic so I am thinking to make use of regular expression. How do I do it?
Example:replace('#hello#','Hi')
Try this:
str.replace(/#[^#]+#/g, 'Hi')
This will remove any sequences of # … # globally with Hi.
Edit Some explanation:
/…/ is the regular expression literal syntax in JavaScript
#[^#]+# describes any sequence of a literal #, followed by one or more (+ quantifier) characters that is not a # (negated charcater class [^#]), followed by a literal #
the g flag in /…/g allows global matches; otherwise only the first match would be replaced
You can use the regex function of jquery to accomplish that...
So find the #'s with a regular expression and afterwards use the replace function with the text you want.
This has nothing to do with jQuery, but just plain old javascript.
var regexp = new RegExp("#([^#]+)#");
text.replace(re, "replacement text");
But what do you mean by generic? How generic do you want to make it?
You can find more information about regular expressions on http://regexp.info including how to use in in Javascript