I have a string: Dkjd(Dk39dD_2=3499(39482ᢕjd
I would like to escape all characters of my choosing (say, _#-) with a backslash. I'm confused how I would do this with String.replace. Can I use the original value in the new value var? Can I use RegEx to do this? Thanks.
No need to use function, use backreference:
source.replace(/([_#\-])/g, "\\$1")
The thing in parentheses can be referenced as $1 in replacement string.
This should be easy with a regex.
var str = 'Dkjd(Dk39dD_2=3499(39482ᢕjd';
str = str.replace(/[_#-]/g, function(match){
return '\\'+match;
});
console.log(str); // Dkjd(Dk39dD\_2=3499(39482&\#6293jd
Simply,
var str = 'Dkjd(Dk39dD_2=3499(39482ᢕjd';
str = str.replace(/([_#-])/g, '\\$1');
Hope it helps.
Related
var Str = "_Hello_";
var newStr = Str.replaceAll("_", "<em>");
console.log(newStr);
it out puts <em>Hello<em>
I would like it to output <em>Hello</em>
but I have no idea how to get the </em> on the outer "_", if anyone could help I would really appreciate it. New to coding and finding this particularly difficult.
Replace two underscores in one go, using a regular expression for the first argument passed to replaceAll:
var Str = "_Hello_";
var newStr = Str.replaceAll(/_([^_]+)_/g, "<em>$1</em>");
console.log(newStr);
NB: it is more common practice to reserve PascalCase for class/constructor names, and use camelCase for other variables. So better str =, ...etc.
I would phrase this as a regex replacement using a capture group:
var str = "_Hello_ World!";
var newStr = str.replace(/_(.*?)_/g, "<em>$1</em>");
console.log(newStr);
I have this string something='http://example.com/something' and how can I replace the something=' with nothing?
when I do str.replace('something='','') I got syntax error. I tried str.replace('something=\'','') and expect to escape the single quote with slash, it doesn't work too.
str.replace('something='','') will of course lead to a syntax error.
Try
str.replace("something='","")
I believe what you are looking for is a replacement of something=' and all ticks (') including the closing one... So you could use this:
var str = "something='http://example.com/something'";
alert(str.replace(/something='(.*)'/, "$1"));
You need to update the str variable with the returned value since String#replace method doesn't update the variable.
str = str.replace('something=\'', '')
Although it's better to use double quotes instead of escaping.
str = str.replace("something='", '')
Well the answer should be very simple.But i am new to the regular expression.
What i want to do is just find and replace :
Eg: iti$%#sa12c##ombina#$tion.43of//.45simp5./l7e5andsp75e$%cial23$#of%charecters
In the above sentence replace the words "of" with "in"
I tried this but didn't get the result, please help me out.
string="iti$%#sa12c##ombina#$tion.43of//.45simp5./l7e5andsp75e$%cial23$#of%charecters";
var string2=string.replace("/(\w*\W*)of(\w*\W*)/g","$1in$2");
console.warn(string2);
Fix the regex literal (no quotes) and use word boundaries (\b, no need to use $1 and $2) :
var string2 = string.replace(/\bof\b/g, "in");
Why not a simple var replaced = yourString.replace(/of/g, 'in');?
Globally replace without using a regex.
function replaceMulti(myword, word, replacement) {
return myword.split(word).join(replacement);
}
var inputString = 'iti$%#sa12c##ombina#$tion.43of//.45simp5./l7e5andsp75e$%cial23$#of%charecters';
var outputString = replaceMulti(inputString, 'of', 'in');
Like this?
str.replace("of","in");
Regular expressions are literals or objects in JavaScript, not strings.
So:
/(\w*\W*)of(\w*\W*)/g
or:
new Regexp("(\\w*\\W*)of(\\w*\\W*)","g");
I'm new to regular expression and have come across a problem.
I want to do a search and replace on a string.
Search for an instance of -- and ' and replace it with - and `, respectively.
Example
Current String: Hi'yo every--body!
Replaced String: Hi`yo every-body!
Any help would greatly be appreciated!
You need.
"Hi'yo every--body!".replace(/--/g, '-').replace(/'/,'`')
Make a function
function andrew_styler(s){return s.replace(/--/g, '-').replace(/'/,'`');}
If you want just replace -- with - use the simplest regexp:
var str = "Hi'yo every--body!";
str = str.replace(/--/g, '-');
The flag g turns the global search on, so that pattern replace all occurances.
#dfsq is correct, regexp is overkill for a couple of simple replaces but for reference.
var s = "Hi'yo every--body!";
s = s.replace(/'/g, "`").replace(/\-{2}/g, "-");
I'm having some troubles getting regex to replace all occurances of a string within a string.
**What to replace:**
href="/newsroom
**Replace with this:**
href="http://intranet/newsroom
This isn't working:
str.replace(/href="/newsroom/g, 'href="http://intranet/newsroom"');
Any ideas?
EDIT
My code:
str = 'photo';
str = str.replace('/href="/newsroom/g', 'href="http://intranet/newsroom"');
document.write(str);
Thanks,
Tegan
Three things:
You need to assign the result back to the variable otherwise the result is simply discarded.
You need to escape the slash in the regular expression.
You don't want the final double-quote in the replacement string.
Try this instead:
str = str.replace(/href="\/newsroom/g, 'href="http://intranet/newsroom')
Result:
photo
You need to escape the forward slash, like so:
str.replace(/href="\/newsroom\/g, 'href=\"http://intranet/newsroom\"');
Note that I also escaped the quotes in your replacement argument.
This should work
str.replace(/href="\/newsroom/g, 'href=\"http://intranet/newsroom\"')
UPDATE:
This will replase only the given string:
str = 'photo';
str = str.replace(/\/newsroom/g, 'http://intranet/newsroom');
document.write(str);