There is the following example of string:
var s = "abcdwq'xx'x";
How can I screen ordinary quotes, i.e. add slash? I tried to use the following code:
s.replace('/(["\'\])/g', "\\$1")
but it doesn't work. Thanks in advance
Don't put the regular expression in quotes, that makes it an ordinary string.
var s = "abcdwq'xx'x";
console.log(s.replace(/(["'])/g, "\\$1"));
Also, you were escaping the ] that ends [.
If you just want to escape single quotes, you don't need the brackets or capture group. Just do:
var s = "abcdwq'xx'x";
console.log(s.replace(/'/g, "\\'"));
Related
var str='\E\\E\10.1.2.154\E\bcs\E\30877_P9999_Adult{2}_02_05_2019_0329p.pdf';
var res=str.replace('\E', '');
I am getting return like this:
\E.1.2.154EcsE877_P9999_Adult{2}_02_05_2019_0329p.pdf
I need to replace all '\E' from string and expecting output like this (\\10.1.2.154\bcs\30877_P9999_Adult{2}_02_05_2019_0329p.pdf). Some body please advise on this . I tried to do several way to fix this. No luck. When I tried with C# it's working fine.
static void Main(string[] args)
{
string str=#"\E\\E\10.1.2.154\E\bcs\E\30877_P9999_Adult{2}_02_05_2019_0329p.pdf";
str=str.Replace(#"\E","");
Console.WriteLine(str);
Console.Read();
}
But, I need it in JavaScript.
var str='\E\\E\10.1.2.154\E\bcs\E\30877_P9999_Adult{2}_02_05_2019_0329p.pdf';
var res=str.replace(/\\E/g, '');
console.log(str, res)
You must use RegExg to eliminate the escape inside a string in JavaScript.
In JavaScript the equivalent of the C# # prefix is String.raw followed by a template literal (notice the backtics).
And to replace all occurrences, not just one, you need to pass a regex to replace with g modifier.
var str=String.raw`\E\\E\10.1.2.154\E\bcs\E\30877_P9999_Adult{2}_02_05_2019_0329p.pdf`;
var res=str.replace(/\\E/g, '');
console.log(res);
NB: The backslash in a regular expression is an escape character, so you need \\ for one literal backslash.
If for some reason you really want to avoid the use of a regex, then there is the split/join trick, but it is a bit slower:
var str=String.raw`\E\\E\10.1.2.154\E\bcs\E\30877_P9999_Adult{2}_02_05_2019_0329p.pdf`;
var res=str.split(String.raw`\E`).join('');
console.log(res);
Older JS engines
For older JS engines that do not support String.raw you need to use standard string literals, which use the backslash as escape character. So then you need to double all of them. But this is only needed when you write the string as a literal. When you get the string via some API, then there is no need to alter the string before doing the replacement:
var str='\\E\\\\E\\10.1.2.154\\E\\bcs\\E\\30877_P9999_Adult{2}_02_05_2019_0329p.pdf';
var res=str.replace(/\\E/g, '');
console.log(res);
I have an html element id that looks like this:
dp__1-2--1-3
I'm trying to use the JavaScript split() function to lop off and return the final '1-3'
My regex skills are poor but a bit of searching around got me to this point:
var myId = "dp__1-2--1-3";
var myIdPostFix = myId.split(/[\-\-]+/).pop();
Unfortunately that returns me only the '3'.
So my question is how do I split double hyphens but NOT single hyphens?
It's the brackets in the regular expression that keeps it from working. A set will match one of any of the characers in it, so [\-\-] is the same as [\-], i.e. matching a single hyphen.
Just remove the brackets:
var myIdPostFix = myId.split(/--/).pop();
or just use the string '--' instead of a regular expression:
var myIdPostFix = myId.split('--').pop();
split accepts a regular expression or a string as the first argument.
You were very close. You can achieve what you want with:
var myIdPostFix = myId.split("--").pop();
I have variable var str as following:
var str = <option value="1">tea</option>;
I would like to make it as below
var quote_str = '<option value="1">tea</option>;'
Is there anyone can help me? Thanks in advance!
Edit:
I have tried the following code,however, it's not correct.
var quote_str = 'str';
I think that you want the semicolon outside the string literal:
var quote_str = '<option value="1">tea</option>';
If you mean that you want apostrophe characters inside the string also, you can use \' to put an apostrophe in a string delimited by apostrophes:
var quote_str = '\'<option value="1">tea</option>\'';
You can also use quotation marks to delimit the string. Then you don't have to escape the apostrophes, but you have to escape the quotation marks:
var quote_str = "'<option value=\"1\">tea</option>'";
If you already have a string, and want to add apostrophes around it, you concatenate strings:
var quote_str = "'" + str + "'";
Escape each single quote with a back-slash:
var quote_str = '\'<option value="1">tea</option>;\''
…or wrap the string in quotes of a different kind (i.e. double quotes), but be sure to escape the inner double quotes as to not unintentionally close the string:
var quote_str = "'<option value=\"1\">tea</option>;'"
late update: now we have template literals, so the whole thing becomes a breeze:
var quote_str = `'<option value="1">tea</option>;'`
You can escape characters in Javascript with the \. If that's your issue
We can use the backslash () escape character to prevent JavaScript from interpreting a quote as the end of the string.
The syntax of \' will always be a single quote, and the syntax of \" will always be a double quote, without any fear of breaking the string.
Using this method, we can use apostrophes in strings built with ".
'We\'re safely using an apostrophe in single quotes.' We can also use quotation marks in strings built with ".
"Then he said, \"Hello, World!\"";
In my case, i'm unable to use the notation of ${} in rendered Javascript inside Python Mako Templates as it's already using ${} for rendering variables in Mako:
# mako template somewhere
var quote_str = `'${str}'`;
So i just wrote a small function:
# app.js ( a real Javascript file )
function singlequote(text) {
return `'${text}'`;
}
And then I use:
# mako template somewhere
var quote_str = singlequote(str);
# So i'm able to also use something like:
let btn = '<button type="button" onclick="update(' + singlequote(myid) + "," + singlequote(mystate) + ')"> Update </button>';
I have posted a problem in the above link - regExpression.test.
Based on that I have done like bellow that also produces an error.
var regExpression=new RegExp("^([a-zA-Z0-9_\-\.]+)$");
alert (regExpression.test("11aa"));
You need to escape your \ since you're declaring it with a string, like this:
var regExpression=new RegExp("^([a-zA-Z0-9_\\-\\.]+)$");
^ ^ add these
You can test it here.
You can also use the literal RegExp syntax /…/:
var regExpression = /^([a-zA-Z0-9_\-\.]+)$/;
By the way: The . does not need to be escaped in character classes anyway. And if you put the range operator at the begin or the end of the character class or immediately after a character range, it doesn’t need to be escaped either:
var regExpression = /^([a-zA-Z0-9_.-]+)$/;
I'm guessing this is a simple problem, but I'm just learning...
I have this:
var location = (jQuery.url.attr("host"))+(jQuery.url.attr("path"));
locationClean = location.replace('/',' ');
locationArray = locationClean.split(" ");
console.log(location);
console.log(locationClean);
console.log(locationArray);
And here is what I am getting in Firebug:
stormink.net/discussed/the-ideas-behind-my-redesign
stormink.net discussed/the-ideas-behind-my-redesign
["stormink.net", "discussed/the-ideas-behind-my-redesign"]
So for some reason, the replace is only happening once? Do I need to use Regex instead with "/g" to make it repeat? And if so, how would I specifiy a '/' in Regex? (I understand very little of how to use Regex).
Thanks all.
Use a pattern instead of a string, which you can use with the "global" modifier
locationClean = location.replace(/\//g,' ');
The replace method only replaces the first occurance when you use a string as the first parameter. You have to use a regular expression to replace all occurances:
locationClean = location.replace(/\//g,' ');
(As the slash characters are used to delimit the regular expression literal, you need to escape the slash inside the excpression with a backslash.)
Still, why are you not just splitting on the '/' character instead?
You could directly split using the / character as the separator:
var loc = location.host + location.pathname, // loc variable used for tesing
locationArray = loc.split("/");
This can be fixed from your javascript.
SYNTAX
stringObject.replace(findstring,newstring)
findstring: Required. Specifies a string value to find. To perform a global search add a 'g' flag to this parameter and to perform a case-insensitive search add an 'i' flag.
newstring: Required. Specifies the string to replace the found value from findstring
Here's what ur code shud look like:
locationClean = location.replace(new RegExp('/','g'),' ');
locationArray = locationClean.split(" ");
njoi'