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
Related
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,
I would like to add to the current string using javascript. Currently, It is working fine but I am doing it in a very dirty way currently. I am basically replacing the WHOLE string instead of just adding to it. Is there a way that I can just add a comma and continue my string?
JS:
var mystring = "'bootstrap'"
console.log(mystring.replace(/bootstrap/g , "'bootstrap', 'bootstrap2', 'bootstrap3'"));
JSFiddle
You can add to the end of a string by using the +=operator. See the docs regarding string operators.
var mystring = "'bootstrap'"
mystring += ", 'bootstrap2'";
mystring += ", 'bootstrap3'";
console.log(mystring);
You can concatenate( + operator ) instead of replace if you just want the second string to get appended to the first string.
var mystring = "'bootstrap'"
var newString = mystring +", "+ "'bootstrap', 'bootstrap2', 'bootstrap3'";
console.log( newString );
What about:
mystring += "'bootstrap2',";
or
var arr = ["str1", "str2", "str3"];
var mystring = arr.map((e)=>{return "'"+e+"'";}).join(",")
Array.map function used to wrap each string with single quates, than you Array.join - used to put "," between members
You can concat strings with the + operator:
var mystring = "'bootstrap'" + ",";
console.log(mystring);
Use an array and the join method.
var arr = [];
var myString = "bootstrap";
arr.push(myString);
arr.push(myString);
arr.push("other string");
arr.push("bootstrap");
// combine them with a comma or something else
console.log(arr.join(', '));
It is not suggested, but if you want to keep symbols and not care about order, and want to only add to a string beginning with 'bootstrap':
myString = "'bootstrap'";
console.log(myString.replace(/^(?='(bootstrap)')/, "'$12', '$13', "));
or you can just use capture group to keep it short
mystring.replace(/'(bootstrap)'/, "'$1', '$12', '$13'")
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]
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];
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