escaping quotes from strings using javascript? - javascript

For instance say I have the string:
var name = 'Mc'Obrian'
I want to be able to escape the the first quote and the last quote only, not the quote used within the name, how can I achieve this in javascript? thanks

The following will properly escape the single quote given your current code:
var name = 'Mc\'Obrian'
I would encourage you to read the following tutorial about strings. If you are interested in performance considerations, read this question.

use \ to escape the single quote like so:
var name = 'Mc\'Obrian'

var name = 'Mc\'Obrian'
or
var name = "Mc'Obrian"

Related

Split String using backslash in javascript

This is my Temp url and I'm trying to get image name
var str='C:\fakepath\alfa_company.png';
my expected out put like this:
var url='alfa_company.png';
In Javascript "\" has a special meaning.
So, it doesn't get included in your resultant string.
Try
let u = String.raw`C:\fakepath\alfa_company.png`;
u.split("\\")[u.split("\\").length-1]
or
let u = String.raw`C:\fakepath\alfa_company.png`;
u.split("\\").pop()
to understand it better go through How can I use backslashes (\) in a string?
You don't need any jQuery for that :)
const path = 'C:\\fakepath\\alfa_company.png';
const filename = path.split('\\').pop(); // alfa_company.png
You need to use double backlashes because JavaScript treats them as escape characters.

MongoDB Search, Skip Special Characters

Suppose a field(version) contains value 1.12.34 or 10.2.3.5 or any string which contains . at any position. So is there is any way to get the document by searching for 11234 or 10235, basically without . in the string.
You can use regex to solve this problem
var str = '1.12.34'
var newString = str.replace(/[^0-9]+/ig, "");
console.log(newString)
Here is an example.
So you can use your version field with replace function using the regex, don't need another field to store version value without dot(.)

Firefox Extension How to pass string type variable into javascript replace regex parameter?

I've been working on firefox extension for several days now and there is one thing I can't solve.
I generate a list of regex and I wanted to pass that string into replace function in javascript (in the regex parameters). Here is the example of the string:
/(https?:\/\/(www\.)?rapidgator\.net\b([-a-zA-Z0-9#:%_\+.~#?&//=]*))/g
/(https?:\/\/(www\.)?ul\.to\b([-a-zA-Z0-9#:%_\+.~#?&//=]*))/g
/(https?:\/\/(www\.)?uploadable\.ch\b([-a-zA-Z0-9#:%_\+.~#?&//=]*))/g
/(https?:\/\/(www\.)?180upload\.com\b([-a-zA-Z0-9#:%_\+.~#?&//=]*))/g
For a convenient way, lets make it this way. I managed to get the file and get the first line of the string and assign it into a variable:
var rapidgator = "/(https?:\/\/(www\.)?rapidgator\.net\b([-a-zA-Z0-9#:%_\+.~#?&//=]*))/g";
I want the string to be a "replace parameter" like this:
var rep = rep.replace(rapidgator,"<a href='$1'>$1</a>");
But I cant get that work.
I've been trying to use RegExp object and that didn't work to.
var rapidgator = new RegExp("(https?:\/\/(www\.)?rapidgator\.net\b([-a-zA-Z0-9#:%_\+.~#?&//=]*))", "g");
How to make that work? Thank you for your advice :)
If you can get the regex, why not let it remain a regex literal?
var rapidgator = /(https?:\/\/(www\.)?rapidgator\.net\b([-a-zA-Z0-9#:%_\+.~#?&//=]*))/g;
If you want to make it through RegExp constructor, make sure you escape \ with another backslash and you don't need delimiters and the second argument takes the flags.
As in
var rapidgator = new RegExp("(https?:\\/\\/(www\\.)?rapidgator\\.net\\b([-a-zA-Z0-9#:%_\+.~#?&//=]*))","g")
You need to escape the backslash one more time when passing your regex within double quotes.
var rapidgator = new RegExp("(https?://(www\\.)?rapidgator\\.net\\b([-a-zA-Z0-9#:%_\\\\+.~#?&/=]*))", "g");
And also to match a backslash, you need to escape it exactly three times.

jQuery / Javascript replace multiple occurences not working

I'm trying to replace multiple occurrences of a string and nothing seems to be working for me. In my browser or even when testing online. Where am I going wrong?
str = '[{name}] is happy today as data-name="[{name}]" won the match today. [{name}] made 100 runs.';
str = str.replace('/[{name}]/gi','John');
console.log(str);
http://jsfiddle.net/SXTd4/
I got that example from here, and that too wont work.
You must not quote regexes, the correct notation would be:
str = str.replace(/\[{name}\]/gi,'John');
Also, you have to escape the [], because otherwise the content inside is treated as character class.
Updating your fiddle accordingly makes it work.
There are two ways declaring regexes:
// literal notation - the preferred option
var re = /regex here/;
// via constructor
var re = new Regexp('regex here');
You should not put your regex in quotes and you need to escape []
Simply use
str = str.replace(/\[{name}\]/gi,'John');
DEMO
While there are plenty of regex answers here is another way:
str = str.split('[{name}]').join('John');
The characters [ ] { } should be escaped in your regular expression.

Help with a regular expression to capture numbers

I need to capture the price out of the following string:
Price: 30.
I need the 30 here, so I figured I'd use the following regex:
([0-9]+)$
This works in Rubular, but it returns null when I try it in my javascript.
console.log(values[1]);
// Price: 100
var price = values[1].match('/([0-9]+)$/g');
// null
Any ideas? Thanks in advance
Try this:
var price = values[1].match(/([0-9]+)$/g);
JavaScript supports RegExp literals, you don't need quotes and delimiters.
.match(/\d+$/) should behave the same, by the way.
See also: MDN - Creating a Regular Expression
Keep in mind there are simpler ways of getting this data. For example:
var tokens = values[1].split(': ');
var price = tokens[1];
You can also split by a single space, and probably want to add some validation.
Why don't you use this?
var matches = a.match(/\d+/);
then you can consume the first element (or last)
my suggestion is to avoid using $ in the end because there might be a space in the end.
This also works:
var price = values[1].match('([0-9]+)$');
It appears that you escaped the open-perens and therefore the regex is looking for "(90".
You don't need to put quotes around the regular expression in JavaScript.

Categories