How to delete part of a string? - javascript

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

Related

Remove string between two variables

I have a string which has some data with a few special characters, Need to remove the data between the desired special char in JavaScript.
The special char would be obtained in a variable.
var desiredChar = "~0~";
And Imagine this to be the Input string:
~0~1|0|20170807|45|111.00|~0~~1~1|0|20170807|50|666.00|~1~~2~1|0|20170807|55|111.00|~2~
So I'm supposed to remove the text in bold.
The desired output is supposed to be-
~1~1|0|20170807|50|666.00|~1~~2~1|0|20170807|55|111.00|~2~
I've tried using "Replace" and "Regex", but as the desired character is being passed in a variable and keeps changing I'm facing difficulties.
You can create your own regex based on whatever the bounding character(s) are that contain the text you want removed, and then replace any text that matches that regex with a blank string "".
The JS below should work for your use case (and it should work for multiple occurrences as well):
var originalText = "~0~1|0|20170807|45|111.00|~0~~1~1|0|20170807|50|666.00|~1~~2~1|0|20170807|55|111.00|~2~";
var desiredChar = "~0~";
var customRegex = new RegExp(desiredChar + ".*?" + desiredChar, "gi");
var processedText = originalText.replace(customRegex, "");
console.log(processedText);
You can build your regex from the constructor with a string input.
var desiredChar = "~0~";
// use the g flag in your regex if you want to remove all substrings between desiredChar
var myRegex = new Regex(desiredChar + ".*" + desiredChar, 'ig');
var testString = "~0~1|0|20170807|45|111.00|~0~~1~1|0|20170807|50|666.00|~1~~2~1|0|20170807|55|111.00|~2~";
testString = testString.replace(myRegex, "");
Given input string you can use .indexOf(), .lastIndexOf() and .slice().
Note, OR character | passed to RegExp constructor should be escaped to avoid RegExp created by passing string interpreting | character as OR | within resulting RegExp passed to .replace().
var desiredChar = "~0~";
var str = "~0~1|0|20170807|45|111.00|~0~~1~1|0|20170807|50|666.00|~1~~2~1|0|20170807|55|111.00|~2~";
var not = str.slice(str.indexOf(desiredChar), str.lastIndexOf(desiredChar) + desiredChar.length);
// escape OR `|`
var res = str.replace(new RegExp(not.replace(/[|]/g, "\\|")), "");
console.log(res)
You can use the RegExp object:
var regexstring = "whatever";
var regexp = new RegExp(regexstring, "gi");
var str = "whateverTest";
var str2 = str.replace(regexp, "other");
document.write(str2);
Then you can construct regexstring in any way you want.
You can read more about it at http://www.regular-expressions.info/javascript.html

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,"\\\\");

how to config RegExp when string contains parentheses

I'm sure this is an easy one, but I can't find it on the net.
This code:
var new_html = "foo and bar(arg)";
var bad_string = "bar(arg)";
var regex = new RegExp(bad_string, "igm");
var bad_start = new_html.search(regex);
sets bad_start to -1 (not found). If I remove the (arg), it runs as expected (bad_start == 8). Is there something I can do to make the (very handy) "new Regexp" syntax work, or do I have to find another way? This example is trivial, but in the real app it would be doing global search and replace, so I need the regex and the "g". Or do I?
TIA
Escape the brackets by double back slashes \\. Try this.
var new_html = "foo and bar(arg)";
var bad_string = "bar\\(arg\\)";
var regex = new RegExp(bad_string, "igm");
var bad_start = new_html.search(regex);
Demo
Your RegEx definition string should be:
var bad_string = "bar\\(arg\\)";
Special characters need to be escaped when using RegEx, and because you are building the RegEx in a string you need to escape your escape character :P
http://www.regular-expressions.info/characters.html
You need to escape the special characters contained in string you are creating your Regex from. For example, define this function:
function escapeRegex(string) {
return string.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&');
}
And use it to assign the result to your bad_string variable:
let bad_string = "bar(arg)"
bad_string = escapeRegex(bad_string)
// You can now use the string to create the Regex :v:

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

JavaScript manipulate string problem

var a
var a = "ACdA(a = %b, ccc= 2r2)";
var b
var b = "\ewfsd\ss.jpg"
Expected outputs:
var c = "ACdA(a = %b, ccc= 2r2, b_holder = \ewfsd\ss.jpg)"
It adds the string b to the end of string a, that's it! But be careful of the ")"
"b_holder " is hard coded string, it's absolutly same in all cases, won't be changed.
Thanks everyone!
You need to do two things:
Concatenate ", b_holder = " to var b, and
Replace ")" in var a with the result of the concatenation.
Since this is homework, I'll leave it to you to figure out which methods to use. Good luck!
Hint: you can either store the result of the concatenation in step (1) in another variable, or you can do it all in one line.
Edit: You also need to concatenate the ")" back onto the end. So maybe three things. :-)
You still don't show any code for what you're doing with a and b to produce c; you are just showing a simple assignment of the expected (desired) value.
You have a problem, however with the value you're assigning to var b -- because the backslash \ is an escape. If you want a backslash in the actual string you need to double it, so your assignment would be
var b = "\\ewfsd\\ss.jpg";
var a = "ACdA(a = %b, ccc= 2r2)";
var b = "\\ewfsd\\ss.jpg"; // need to escape the backslash for RegExp replace
var re = /\)$/;
var c = a.replace(re, ", b_holder = "+b+"\)");

Categories

Resources