Replacing Backslashes in Javascript - 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,"\\\\");

Related

How to get the number before percentage symbol in a string?

These are some possible strings. The number percentage is always at beginning.
var string = "0.5% - corresponding something" or var string = "23% - correspondig something";
I need to get the the entire number before the percentage symbol.
I already tried some solutions in cutting the string, but the problem is that the number doesn't have always the same length.
Can you help me?
There are several solutions. For example:
let antani = "23% - correspondig something";
// using parseFloat
console.log(parseFloat(antani));
// using split
console.log(antani.split("%")[0]);
first split string using '%'
<script>
var str = "0.5% - corresponding something";
var res = str.split("%");
number=res[0];
console.log(res[0]);
</script>
Using Regular expression you can match the numbers (including dots) before the percentage sign.
Example:
var text = "0.5% - corresponding something";
var percentageRegex = /([0-9\.]+).+/;
var matches = percentageRegex.exec(text);
console.log(matches[1]);

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);

Remove quotes from array beginning & trailing

How to remove the leading & trailing double quotes outside the array.
var data = [{"name":"myName" ,"address": "myAddress" }];
alert(data[0].name)
If you want to convert a string representation of JSON data into an Javascript object:
http://jsfiddle.net/H2yN6/191/
var str = "[{\"name\":\"myName\" ,\"address\": \"myAddress\" }]";
var data = JSON.parse(str);
alert(data[0].name);
But if you really do want to remove some leading and/or trailing characters, you can use substring():
http://jsfiddle.net/H2yN6/193/
var str = "\"[{\"name\":\"myName\" ,\"address\": \"myAddress\" }]\"";
var str2 = str.substring(1, str.length - 1);
alert(str2);

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

String replace not working?

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');

Categories

Resources