Replace comma by double quote in javascript - javascript

How to replace comma by double quote in javascript?
For example: "c,C#,JavaScript" should be like "C","C#","JavaScript"

str = '"c,C#,JavaScript"';
str = str.split(',').join('","');
That would result in "c","C#","JavaScript"

var original = '"c,C#,JavaScript"';
var quoted = original.replace(/,/g, '","'); // "c","C#","JavaScript"

Just to toss it in there, you could also .split() and .join(), like this:
var oldString = '"c,C#,JavaScript"';
var newString = oldString.split(',').join('","');
You can test it here.

You can do this:
str = "c,C#,JavaScript";
str = str.replace(/,/g, '"');
Result:
c"C#"JavaScript

Related

replace number from the middle using javascript / node js

var str = "demo-test-1-0-4_testing.pdf";
I have a string i want to replace with word verified
so my expected value look like this
str= demo-test-verified_testing.pdf
I can do this if the numbers are written in last like this
var str = "demo-test-1-0-1";
str = str.replace(/(?!(-| |_))(\d|-|\.)+\s*$/, "verified");
console.log(str);
but in middle I don't know
as my expected value is like this demo-test-verified_testing.pdf as i want to replace 1-0-1 i can use replace function but every time version get updated so number get changed that's why i cant use replace function
This should be enough:
var str = "demo-test-1-0-4_testing.pdf";
str = str.replace(/(-\d+)+/,"-verified")
console.log(str)
str = "demo-test-1-0-1"
str = str.replace(/(-\d+)+/,"-verified")
console.log(str)
var str = "demo-test-1-0-41_testing.pdf";
str = str.replace(/(-\d+)+/,"-verified")
console.log(str)

remove second comma from a string in javascript

I have an string as:
0123456789,, 0987654213 ,, 0987334213,, ......
How can I convert this into
0123456789, 0987654213, 0987334213, ......
i.e I want to remove the second comma
You can do it very simply, like this using regex.
var str = "0123456789,,0987654213,,0987334213,,,,,9874578";
str=str.replace(/,*,/g,',');
console.log(str)
var str = "0123456789,, 0987654213 ,, 0987334213,, ......"
console.log(str.replace(/\,+/g,","));
This will replace all occurrences of consecutive commas with a single comma:
str.replace(/,+/g, ',');
You can use replace() method with regular expression with g flag to replace all instances ',,' with ',':
str.replace(/,,/g, ",");
Here's a simple example
var str = '0123456789,, 0987654213 ,, 0987334213';
str = str.replace(/,,/g, ",");
console.log(str);
var str = "0123456789,, 0987654213 ,, 0987334213,,"
str = str.split(",,").join(",")
console.log(str);
There is a replace method for String.
You can replace ',,' with a ','.
An example:
var str = "0123456789,, 0987654213,, 0987334213,";
var newStr = str.replace(/,,/g,','));
The output:
0123456789, 0987654213, 0987334213,

Remove double quotes from javascript string

var tabData='[{type:"line",showInLegend:true,dataPoints:[{"x":new Date("2017-02-25"),"y":20},{"x":new Date("2016-02-25"),"y":15}]},{type:"line",showInLegend:true,dataPoints:[{"x":new Date("2017-02-25"),"y":20},{"x":new Date("2016-02-25"),"y":15}]}]';
I want to remove double quotes from only new Date(" ") wherever new Date (" ") appears.
like- new Date(2017-02-25) ..
var tabData='[{type:"line",showInLegend:true,dataPoints:[{"x":new Date("2017-02-25"),"y":20},{"x":new Date("2016-02-25"),"y":15}]},{type:"line",showInLegend:true,dataPoints:[{"x":new Date("2017-02-25"),"y":20},{"x":new Date("2016-02-25"),"y":15}]}]';
var output = tabData.replace(/new Date\("([^"]*)"\)/g, 'new Date($1)');
console.log(output);
Or assign the result back to tabData instead of to a new variable.
Further reading:
string .replace()
Regular expressions at MDN
Regular-Expressions.info
I have no idea why you would want to do this, but you can use String#replace to do what you asked:
var tabData = '[{type:"line",showInLegend:true,dataPoints:[{"x":new Date("2017-02-25"),"y":20},{"x":new Date("2016-02-25"),"y":15}]},{type:"line",showInLegend:true,dataPoints:[{"x":new Date("2017-02-25"),"y":20},{"x":new Date("2016-02-25"),"y":15}]}]'
tabData = tabData.replace(/new Date\("([^"]*)"\)/g, 'new Date($1)')
console.log(tabData)
try this,
var tabData='[{type:"line",showInLegend:true,dataPoints:[{"x":new Date("2017-02-25"),"y":20},{"x":new Date("2016-02-25"),"y":15}]},{type:"line",showInLegend:true,dataPoints:[{"x":new Date("2017-02-25"),"y":20},{"x":new Date("2016-02-25"),"y":15}]}]';
tabData=tabData.replace(/Date\("/g, 'Date(');
tabData.replace(/\"\)/g, ')');
Ouptut:
"[{type:"line",showInLegend:true,dataPoints:[{"x":new
Date(2017-02-25),"y":20},{"x":new
Date(2016-02-25),"y":15}]},{type:"line",showInLegend:true,dataPoints:[{"x":new
Date(2017-02-25),"y":20},{"x":new Date(2016-02-25),"y":15}]}]"
You can try doing it like this:
var regEx = /new\sDate\("(\d*-\d*-\d*)"\)/g;
var str = ""; //your string as mentioned above
str = str.replace(regEx,"new Date($2)")
For eg,
var str = 'dsdsadsadsanew Date("12121212")dsdsdsads'
str = str.replace(regEx,"new Date($1)")
//output: "dsdsadsadsanew Date(12121212)dsdsdsads"
For global scope in entire string, do as:
var regEx = /new\sDate("(\d*-\d*-\d*)")/g

Pattern match to get string between brackets

I am trying to find whatever string is in between the aggregate() and find(). Below is my code.
var str1 = 'aggregate([{$group:{_id:{state:"$state",city:"$city"},sum:{$sum:"$pop"}}},{$sort:{sum:1}},{$group:{_id:"$_id.state",smallestcity:{$first:"$_id.city"},smallest:{$first:"$sum"},largestcity:{$last:"$_id.city"},largest:{$last:"$sum"}}}])'
var str2 = 'find({awards:{$elemMatch:{award:"Turing Award",year:{$gt:1980}}}}).limit(0)'
var matchPharse = /((.*))/;
var result = str1.match(matchPharse);
console.log(result);
I am getting the result always the whole string instead of
[{$group:{_id:{state:"$state",city:"$city"},sum:{$sum:"$pop"}}},{$sort:{sum:1}},{$group:{_id:"$_id.state",smallestcity:{$first:"$_id.city"},smallest:{$first:"$sum"},largestcity:{$last:"$_id.city"},largest:{$last:"$sum"}}}]
I am searching for something like this
try this pattern instead:
var matchPharse = /((\[.*\]))/;
((\[.*?\]))
You Should use a non greedy expression.
Try the following RegEx:
var matchPharse= /\((.*)\)/g;
Matches any sequence between ().
This is a DEMO.
var str1 = 'aggregate([{$group:{_id:{state:"$state",city:"$city"},sum:{$sum:"$pop"}}},{$sort:{sum:1}},{$group:{_id:"$_id.state",smallestcity:{$first:"$_id.city"},smallest:{$first:"$sum"},largestcity:{$last:"$_id.city"},largest:{$last:"$sum"}}}])'
var str2 = 'find({awards:{$elemMatch:{award:"Turing Award",year:{$gt:1980}}}}).limit(0)'
var matchPharse = /\((.*)\)/;
var result = str1.match(matchPharse);
alert(result);
You just need to escape the outside parentheses. Try:
var matchPharse = /\((.*)\)/;
For just the content inside the parentheses use result[1]

jQuery string split the string after the space using split() method

my code
var str =$(this).attr('id');
this will give me value == myid 5
var str1 = myid
var str2 = 5
i want something like this ..
how to achieve this using split method
var str =$(this).attr('id');
var ret = str.split(" ");
var str1 = ret[0];
var str2 = ret[1];
Use in-built function: split()
var source = 'myid 5';
//reduce multiple places to single space and then split
var splittedSource = source.replace(/\s{2,}/g, ' ').split(' ');
console.log(splittedSource);
​
Note: this works even there is multiple spaces between the string groups
Fiddle: http://jsfiddle.net/QNSyr/6/
One line solution:
//<div id="mypost-5">
var postId = this.id.split('mypost-')[1] ); //better solution than the below one!
-OR-
//<div id="mypost-5">
var postId = $(this).attr('id').split('mypost-')[1];

Categories

Resources