String replace not working? - javascript

I've been trying to replace to following string using the following...
var r = response.replace('var s = getService().getValue(\"join\")', 'null');
However, the String remains un changed and I can't understand why. The String itself takes the following format..
{"r":[],"c":true,"c":{"tags":
[],"":3023,"s":".src.util.S#6f4e9e57","class":"class
src.util.dtos.DTO","Type":"public","c":"m","s":0,"de
fault":false,"id":544,"d":"","n":4,"na":"S","tagString":"","Pages":5},"results":[],"q":"","msg":"var
s = getService().getValue(\"join\")
The actual string itself is a little longer but I hope you get the idea from that abstract.

If your
var s = getService().getValue(\"join\")
part is a JavaScript code inside a JSON string, then you need to quote them again before replacing.
var r = response.replace('var s = getService().getValue(\\"join\\")', 'null');

It's because the double quotes escaped in JSON are not supposed to be escaped inside a single-quoted string.
So, instead of:
var r = response.replace('var joinstakqueries = getService().getValue(\"join\")', 'null');
try:
var r = response.replace('var joinstakqueries = getService().getValue("join")', 'null');

Related

Javascript regexp replace capturing

I'm trying to use regex in a Nodejs app. I usually use it in Python and it seems to have some differences.
Here is the problem :
I have this string \newcommand{\hello}{#replace} and I want to replace #replace by REPLACED in the second curly bracelets ONLY when I found \hello in the first curly bracelets. So the expected result is : \newcommand{\hello}{REPLACED}
I try this:
r = new RegExp('\\newcommand{\\hello}{(.*?)}');
s = '\\newcommand{\\hello}{#replace}';
s.replace(r, 'REPLACED');
But nothing is replaced... any clue?
r = new RegExp(/\\newcommand{\\hello}{#replace}/);
s = '\\newcommand{\\hello}{#replace}';
let a = s.replace(r, '\\newcommand{\\hello}{REPLACED}');
console.log(a)
Output would be : "\newcommand{\hello}{REPLACED}"
I'm not sure if I understood the question correctly. Is this what you're looking for?
function replaceWith(myReplacement) {
var original = "\\newcommand{\\hello}{#replace}";
var regex = "{\\hello}{#replace}";
return original.replace(regex, `{\\hello}{${myReplacement}}`)
};
console.log(replaceWith("World"));
You don't need regex at all to perform this kind of operation. You can simply use string at first parameter:
s = '\\newcommand{\\hello}{#replace}';
s.replace('#replace', 'REPLACED'); // => "\newcommand{\hello}{REPLACED}"

convert String to array in javascript "datastatusMonthly[0]"

datastatusMonthly[0] - This is my String in javascript
If i print this, it is printing as same string.
How do i get the value of '0' index in array datastatusMonthly using this above string?
Any help please?
You can use eval. The eval function will evaluate your string. JS bin here https://jsbin.com/guqoqukoqa/edit?js,console
Solution without eval, which is evil, using regex with group:
var datastatusMonthly = [3];
var text = 'datastatusMonthly[0]';
var regex = /(datastatusMonthly)\[([0-9]+)\]/g;
var match = regex.exec(text);
var arrayName = match[1];
var arrayIndex = match[2];
console.log(window[arrayName][arrayIndex]);
This dose't have to be in a String i guess. correct me if i am not understanding it properly
var fistElement = datastatusMonthly[0];
This link might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Accessing_array_elements

How to delete part of a string?

How to delete part of a string in JavaScript?
I tried
var a = "C:\mnt\c\User\Foo\Bar";
var b = a.replace("mnt\c", "");
But it does not work
Your search pattern doesn't include a backslash, even though it looks like it does. This is because you need to escape it.
var a = "C:\\mnt\\c\\User\\Foo\\Bar";
var b = a.replace("mnt\\c", "");
console.log(b);
I also changed the a variable to escape the backslashes, though this is only because it's required in a string literal. I assume the input source is from elsewhere.
You just need to escape the \ in the string.
//var a = "C:\mnt\c\User\Foo\Bar";
var a = "C:\\mnt\\c\\User\\Foo\\Bar";
console.log(a);
var b = a.replace("mnt\\c", "");
console.log(b);

How can we split a string with out quotes using jQuery or Javascript

I have a string coming from database like Agree,don't,partially,completely. without quotes. I have taken this in to a variable and I have splitted.
var x= Agree,Dont,Partially,completely;
var split = x.split(',');
but the code is not working then i have inserted the quotes to the string like.
var x = "\'" + answer coming from database + "\'";
var split = x.split(',');
Then the code works out but i get the ans as
x[0] = "Agree,x[1]=Dont ... x[3] = Completely"
My question is how can i split the value without appending the quotes to the x.
Hope you understand my question
Reagrds,
Sri
var x= "Agree,Dont,Partially,completely";
var split = x.split(',');
alert(split[0]+" "+split[1]);
Your syntax should be:
var x = 'Agree,Dont,Partially,completely';
var split = x.split(',');
The quotation marks define the start and end of the string above.
var x= "Agree,Dont,Partially,completely";
var split = x.split(',');
This should give what you need

Replacing Backslashes in Javascript

How can I convert the following string from
var x = "F:\RSSIMS\database\"
to:
var x = "F:\\RRSIMS\\database\\"
Thanks for all your help.
var x = "F:\RSSIMS\database\" is already invalid. You need to escape your backslashes.
var x = "F:\\RSSIMS\\database\\";//stores the string `F:\RSSIMS\database\` in x.
If you want double-slashes now, do the below to replace all the single slashes by double ones.
x = x.replace(/\\/g,"\\\\");

Categories

Resources