This question already has answers here:
Javascript replace issue with $ [duplicate]
(7 answers)
Closed 8 years ago.
I would like to replace all occurences of a particular string with $$ in javascript.
However when I attempt the replace it only replaces a single character
http://jsfiddle.net/PrZ9y/
text = "sfsd";
text =text.replace(/sf/gi,"\$\$");
document.getElementById("x").innerHTML=text;
This for example outputs $sd. The correct output should be $$sd
I also tried
text =text.replace(/sf/gi,"$$");
Since $ is reserved for regular expression, doubling-up on $ will escaped it's intended purpose.
text = "sfsd";
text = text.replace(/sf/gi,"$$$$");
document.getElementById("x").innerHTML = text;
Dollar sign has a special meaning, it means the number of captured group when used in conjuction with a number, like $1. $$ is just the literal for $, the first $ will play the role of escape character. Use $$$$ for $$.
More info:
http://es5.github.io/#x15.5.4.11
Try putting your html as:
<div id="x">sf sd</div>
<div id="wmd-input"></div>
Then, change your code to:
text = document.getElementById("x").innerHTML;
text = text.replace(/sf/gi,"$$$$");
document.getElementById("wmd-input").innerHTML = text;
jsfiddle.
As #Mr Polywhirl noted, you need to use another $ to escape the $ and get one literal $ sign.
$$ is reserved just like $& is reserved, so use three of them:
text=text.replace(/sf/gi,"$$$");
Related
This question already has answers here:
How can I use Unicode-aware regular expressions in JavaScript?
(11 answers)
Closed 4 years ago.
How would I remove all Unicode from this string【Hello!】★ ああああ
I need to remove all the "weird" symbols (【, ★, 】) and keep "Hello!" and "ああああ". This needs to work for all languages not just Japanese.
You want to remove characters within the Unicode categories Other Symbol, Combining Symbol, and Enclosing Mark, but leave those from other categories.
Using regular expressions, those match the classes \p{So}, \p{Sk} and \p{Me}, respectively. You might for example use XRegExp.replace().
I have found a solution. Using XRegEXP, I was able to use PHP's \p{Common} in node.
const xreg = require('xregexp');
let str = '【Hello!】★ ああああ】';
let regex = new xreg('\\p{Common}', 'g');
let res = xreg.replace(str, regex, ' ');
console.log(res); // Hello ああああ
This question already has answers here:
Javascript string replace method. Using matches in replacement string
(2 answers)
Closed 3 years ago.
I need to put every non-alphabetic character between spaces.
I want to do this using RegExp, and I understand it enouch to select them all (/(^a-zA-Z )/g).
Is there a way to use the original match inside the replace?
(something like)
str.replace(/(^a-zA-Z )/g,/ \m /);
If not I will just loop over all of them, but I really want to know it it is possible.
Yes. You can give the String.prototype.replace() function a RegExp as it's search. You can also give it a function to handle replacing.
The function will give you the match as the first parameter, and you return what you want to change it to.
const original = 'a1b2c';
const replaced = original.replace(/([^a-z])/gi, match => ` ${match} `);
console.log(replaced);
If you just need to do something simple, you can also just use the $n values ($1, $2, etc) to replace based on the selected group (the sets of parentheses).
const original = 'a1b2c';
const replaced = original.replace(/([^a-z])/gi, ' $1 ');
console.log(replaced);
Yes, it is possible. You can use regex with group:
var text = '2apples!?%$';
var nextText = text.replace(/([^a-zA-Z])/g, ' $1 ');
console.log(nextText);
You can check replace function on this link
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 6 years ago.
Hi I have a problem here. I am trying to replace all instances of + character in a string using javascript. What happens is that only the first instance is being changed.
Here is my code:
var keyword = "Hello+Word%+";
keyword = keyword.replace("+", encodeURIComponent("+"));
alert(keyword);
The output is Hello%2BWord%+ when it should be Hello%2BWord%%2B because there are 2 instances of +.
You can check this on : http://jsfiddle.net/Wy48Z/
Please help. Thanks in advance.
You need the global flag.
Fixed for you at http://jsfiddle.net/rtoal/Wy48Z/1/
var keyword = "Hello+Word%+";
keyword = keyword.replace(/\+/g, encodeURIComponent("+"));
alert(keyword);
The javascript regex, which is done by putting the expresison inbetween two forward slashes like: /<expression/
If you want to replace all, simply append a g after the last one like:
/<expression/g
In your case, it would be /\+/g
The cross-browser approach is to use a regexp with the g (global) flag, which means "process all matches of the pattern, not just the first":
keyword = keyword.replace(/\+/g, encodeURIComponent("+"));
Notice I prefix the plus sign with a backslash because it would otherwise have the special meaning of "match one or more of the preceding thing".
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regular Expression to find a string included between two characters, while EXCLUDING the delimiters
I have a function where I have to get text which is enclosed in square brackets but not brackets for example
this is [test] line i [want] text [inside] square [brackets]
from the above line I want words:
test
want
inside
brackets
I am trying with to do this with /\[(.*?)\]/g but I am not getting satisfied result, I get the words inside brackets but also brackets which are not what I want
I did search for some similar type of question on SO but none of those solution work properly for me here is one what found (?<=\[)[^]]+(?=\]) this works in RegEx coach but not with JavaScript. Here is reference from where I got this
here is what I have done so far: demo
please help.
A single lookahead should do the trick here:
a = "this is [test] line i [want] text [inside] square [brackets]"
words = a.match(/[^[\]]+(?=])/g)
but in a general case, exec or replace-based loops lead to simpler code:
words = []
a.replace(/\[(.+?)\]/g, function($0, $1) { words.push($1) })
This fiddle uses RegExp.exec and outputs only what's inside the parenthesis.
var data = "this is [test] line i [want] text [inside] square [brackets]"
var re= /\[(.*?)\]/g;
for(m = re.exec(data); m; m = re.exec(data)){
alert(m[1])
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
JavaScript replace all / with \ in a string?
I have a date='13/12/2010' I want to replace this '/' to '_' or something else.
I read this But I do not know how can that applied for my case .
Use a global RegEx (g = global = replace all occurrences) for replace.
date = date.replace(/\//g, '_');
\/ is the escaped form of /. This is required, because otherwise the // will be interpreted as a comment. Have a look at the syntax highlighting:
date = date.replace(///g, '_');
One easiest thing :)
var date='13/12/2010';
alert(date.split("/").join("_")); // alerts 13_12_2010
This method doesn't invoke regular expression engine and most efficient one
You can try escaping the / character like this -
date.replace( /\//g,"_");