I have a JavaScript variable:
var text = "http://example.com"
Text can be multiple links. How can I put '' around the variable string?
I want the strings to, for example, look like this:
"'http://example.com'"
var text = "\"http://example.com\"";
Whatever your text, to wrap it with ", you need to put them and escape inner ones with \. Above will result in:
"http://example.com"
var text = "http://example.com";
text = "'"+text+"'";
Would attach the single quotes (') to the front and the back of the string.
I think, the best and easy way for you, to put value inside quotes is:
JSON.stringify(variable or value)
You can add these single quotes with template literals:
var text = "http://example.com"
var quoteText = `'${text}'`
console.log(quoteText)
Docs are here. Browsers that support template literals listed here.
Try:
var text = "'" + "http://example.com" + "'";
To represent the text below in JavaScript:
"'http://example.com'"
Use:
"\"'http://example.com'\""
Or:
'"\'http://example.com\'"'
Note that: We always need to escape the quote that we are surrounding the string with using \
JS Fiddle: http://jsfiddle.net/efcwG/
General Pointers:
You can use quotes inside a string, as long as they don't match the
quotes surrounding the string:
Example
var answer="It's alright";
var answer="He is called 'Johnny'";
var answer='He is called "Johnny"';
Or you can put quotes inside a string by using the \ escape
character:
Example
var answer='It\'s alright';
var answer="He is called \"Johnny\"";
Or you can use a combination of both as shown on top.
http://www.w3schools.com/js/js_obj_string.asp
let's think urls = "http://example1.com http://example2.com"
function somefunction(urls){
var urlarray = urls.split(" ");
var text = "\"'" + urlarray[0] + "'\"";
}
output will be text = "'http://example1.com'"
In case of array like
result = [ '2015', '2014', '2013', '2011' ],
it gets tricky if you are using escape sequence like:
result = [ \'2015\', \'2014\', \'2013\', \'2011\' ].
Instead, good way to do it is to wrap the array with single quotes as follows:
result = "'"+result+"'";
You can escape " with \
var text="\"word\"";
http://jsfiddle.net/beKpE/
Lets assume you have a bunch of urls separated by spaces. In this case, you could do this:
function quote(text) {
var urls = text.split(/ /)
for (var i = 0; i < urls.length; i++) urls[i] = "'" + urls[i] + "'"
return urls.join(" ")
}
This function takes a string like "http://example.com http://blarg.test" and returns a string like "'http://example.com' 'http://blarg.test'".
It works very simply: it takes your string of urls, splits it by spaces, surrounds each resulting url with quotes and finally combines all of them back with spaces.
var text = "\"http://www.example1.com\"; \"http://www.example2.com\"";
Using escape sequence of " (quote), you can achieve this
You can place singe quote (') inside double quotes without any issues
Like this
var text = "'http://www.ex.com';'http://www.ex2.com'"
Another easy way to wrap a string is to extend the Javascript String prototype:
String.prototype.quote = function() { return "\"" + this + "\""; };
Use it like this:
var s = "abc";
console.log( "unwrapped: " + s + ", wrapped: " + s.quote() );
and you will see:
unwrapped: abc, wrapped: "abc"
This can be one of several solutions:
var text = "http://example.com";
JSON.stringify(text).replace('\"', '\"\'').replace(/.$/, '\'"')
Related
I have the below input
var input = (a-d){12-16},(M-Z){5-8},[#$%!^,12+-,23^!]
I need to remove the comma within the square brackets such that the final output will be
var output = (a-d){12-16},(M-Z){5-8},[#$%!^12+-23^!]
By solution
function test()
{
var input = '(a-d){12-16},(M-Z){5-8},[#$%!^,12+-,23^!]'; //input string
var splitByFirstBracket = input.split("["); //split the input by [ character
//merge the arrays where the second array is replaced by '' for ','
var output = splitByFirstBracket[0] + '[' + splitByFirstBracket[1].replace(/,/g,'');
alert(output);
}
It is providing the output correctly. Is there any better way - I am open both for JavaScript and JQuery.
Thanks in advance
You can use a regular expression replacement. The replacement can be a function, which receives the part of the input that was matched by the regexp, and then it can calculate the replacement. In this case, it would use another replace call to remove the commas.
var input = '(a-d){12-16},(M-Z){5-8},[#$%!^,12+-,23^!]'; //input string
var output = input.replace(/\[.*?\]/g, function(match) {
return match.replace(/,/g, '');
});
console.log(output);
I have a dynamic list of emails which are separated by comma, and I want to put all in single comma
In example:
(a#zyz.com,b#xyz.com,c#xyz.com,d#xyz.com) to ('a#zyz.com','b#xyz.com','c#xyz.com','d#xyz.com')
I tried to resolve it, but didn't succeed.
Supposing you want to enclose emails with single quotes, you can split on , and then Array.map and then Array.join
var str = "a#zyz.com,b#xyz.com,c#xyz.com,d#xyz.com";
str = str.split(",").map(function(str){
return "'" + str + "'"; // add quotes
}).join(",") // join the array by a comma
use split method of javascript.
E.g.
var str = "a#zyz.com,b#xyz.com,c#xyz.com,d#xyz.com";
var res = str.split(',');
I use split(" ") for whitespace and use split(/(<[^>]*>)/) for html tag in string.
but i have to use together. i want to divide a string by whitespace and html tag and put it into an array. i don't want to lose anything. string, html tag both.
whitespace is \s and html tag is split(/(<[^>]*>)/) and i used like this new RegExp("\s+(<[^>]*>)", "g");
but it doesn't work.
var htmlTagRegex = new RegExp("\s+(<[^>]*>)", "g");
var str = ""<div class="tab0">CSS code formatter</div><div class="tab2">CSS code compressor</div>";
var myArray = str.split(htmlTagRegex);
if(myArray != null){
for ( i = 0; i < myArray.length; i++ ) {
var result = "myArray[" + i + "] = " + myArray[i]+"<br />";
$(".tt-sns-clear").append(result);
}
}
Not 100% sure what you are trying to do but it looks like the space needs to be optional like \s* instead of \s+
Something like : \s*(<[^>]*>)
And since you are not concatenating a string to your Regex better is:
var htmlTagRegex =/\s*(<[^>]*>)/g
The input string is not using double quotes correctly and it is not compiling either, you need to mix single and double quotes:
'<div class="tab0">CSS code formatter</div><div class="tab2">CSS code compressor</div>';
All together looks like
var htmlTagRegex =/\s*(<[^>]*>)/g
var str = '<div class="tab0">CSS code formatter</div><div class="tab2">CSS code compressor</div>';
var myArray = str.split(htmlTagRegex);
//outputs ["", "<div class="tab0">", "CSS code formatter", "</div>", "", "<div class="tab2">", "CSS code compressor", "</div>", ""]
And it seems to work fine on my end.
string is from mysql. And that is inclueded html tag.
And I used like this: var htmlTagRegex =/\s|(<[^>]*>)/g;
but result is not what i want.
["","CSS","code","formatter","<div class="tab0"></div>","","CSS","code","compressor","<div class="tab1"></div>"]
I want like this
["<div class="tab0">","CSS","code","formatter","","<div class="tab1">","CSS","code","compressor","</div>"]
I have a string like
var test = "1,2,3,4";
I need to append single quotes (' ') to all characters of this string like this:
var NewString = " '1','2','3','4' ";
Please give me any suggestion.
First, I would split the string into an array, which then makes it easier to manipulate into any form you want. Then, you can glue it back together again with whatever glue you want (in this case ','). The only remaining thing to do is ensure that it starts and ends correctly (in this case with an ').
var test = "1,2,3,4";
var formatted = "'" + test.split(',').join("','") + "'"
var newString = test.replace(/(\d)/g, "'$1'");
JS Fiddle demo (please open your JavaScript/developer console to see the output).
For multiple-digits:
var newString = test.replace(/(\d+)/g, "'$1'");
JS Fiddle demo.
References:
Regular expressions (at the Mozilla Developer Network).
Even simpler
test = test.replace(/\b/g, "'");
A short and specific solution:
"1,2,3,4".replace(/(\d+)/g, "'$1'")
A more complete solution which quotes any element and also handles space around the separator:
"1,2,3,4".split(/\s*,\s*/).map(function (x) { return "'" + x + "'"; }).join(",")
Using regex:
var NewString = test.replace(/(\d+)/g, "'$1'");
A string is actually like an array, so you can do something like this:
var test = "1,2,3,4";
var testOut = "";
for(var i; i<test.length; i++){
testOut += "'" + test[i] + "'";
}
That's of course answering your question quite literally by appending to each and every character (including any commas etc.).
If you needed to keep the commas, just use test.split(',') beforehand and add it after.
(Further explanation upon request if that's not clear).
Here is the Original string :
var str = " a vartiable";
and I need this part:
str = "https://sjobs.brassring.com/1033/ASP/TG/cim_jobdetail.asp?SID=^cJgiKPhGBHyn5VRSb9gbJg0K2T88FrLqHyAtd6hd5pJ7JeXxNyq0VatKCq3jYWp/&jobId=385594&type=hotjobs&JobReqLang=141&JobSiteId=5239&JobSiteInfo=385594_5239&GQId=0";
In other words, I need to remove the tag <a> and the document.href value
Thanks guys.
How about:
var str = " a vartiable";
str.replace(/^<a href="(https.*?)cim_home\.asp.*?'(cim_jobdetail\.asp.*)'.*$/, "$1$2");
produces:
"https://sjobs.brassring.com/1033/ASP/TG/cim_jobdetail.asp?SID=^cJgiKPhGBHyn5VRSb9gbJg0K2T88FrLqHyAtd6hd5pJ7JeXxNyq0VatKCq3jYWp/&jobId=385594&type=hotjobs&JobReqLang=141&JobSiteId=5239&JobSiteInfo=385594_5239&GQId=0"
Something simple like the following should work...
href="(.*?)"
here's the code u want:
var str = ' a vartiable'
var url = /\"(.*?)\"/str
that's how you match, here's how you strip it out:
str.replace(/\"(.*?)\"/, "$1");
the \"(.*?)\" gives the first minimal set of characters between two " characters the id of $1 then the second argument to the replace function tells it to replace the whole string with what's contained in $1
Also, if you use jQuery, this becomes pretty trivial:
var url = $("a").attr("href");