JavaScript manipulate string problem - javascript

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

Related

Get specific letters between two symbols

I'm trying to get the letters between two specified symbols.
Example
var a = 'Testing code https://example.com/ABC-BAC tralala'; // I would need to get ABC
var b = 'Depends on x \n https://example.com/CADER-BAC'; // I would need to get CADER
var c ='lots of examples: \n example.com/CAB-BAC'; // I would need to get CAB
var d ='lots of examples: \n https://abc.example.com/CAB-BAC'; // I would need to get CAB
My main problem is that I need a unique syntax that would work for both https:// version or without any https://, therefore using indexOf on '/' wouldn't work that well.
I've tried regex but of course, that only fits one solution:
let regex = /^https://example\.com/[a-zA-Z]+-BAC$/i;
return regex.test(input);
Any help is appreciated, thank you!
You may use match here:
var input = "https://example.com/CADER-BAC";
var code = input.match(/^.*\/([^-]+)-.*$/);
console.log(code[1]);
You can use str.substring in combination of str.lastIndexOf:
const a = 'Testing code https://example.com/ABC-BAC tralala'; // I would need to get ABC
const b = 'Depends on x \n https://example.com/CADER-BAC'; // I would need to get CADER
const c = 'lots of examples: \n example.com/CAB-BAC'; // I would need to get CAB
const getSubstring = (str) => {
return str.substring(
str.lastIndexOf('example.com/') + 'example.com/'.length,
str.lastIndexOf('-'))
}
console.log(getSubstring(a), getSubstring(b), getSubstring(c))

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 to copy text between 2 symbols[keywords] in JavaScript?

How do I replace text between 2 symbols, in my case "." and "/" (without the quotes)
While doing some research at Stack, I found some answers, but they didn't help much.
I think the problem is using common operators as a part of string.
I tried using split
var a = "example.com/something"
var b = a.split(/[./]/);
var c = b[0];
I also tried this:
srctext = "example.com";
var re = /(.*.\s+)(.*)(\s+/.*)/;
var newtext = srctext.replace(re, "$2");
But the above 2 didn't seem to work.
I would be real glad if someone were to solve the question and please explain the use of escape sequences using an example or two. For a side note, I tried Googling it up but the data was not too helpful for me.
Try this code.
var a = "example.com/something";
var textToChange = 'org';
var result = a.replace(/(\.)([\w]+)(\/)/, '$1' + textToChange + '$3');
result will be example.org/something
$1 equals .
$2 is the string you want to change
$3 equals /
Currently I only assumed the text you want to change is mixture of alphabets. You can change [\w]+ to any regular expression to fit the text you want to change.
The example given in your question will work if you make a small change
var a = "example.com/something"
var b = a.split(/[./]/);
var c = b[1];
alert(c);
b[1] will give you string between . and / not b[0]
You can use RegEx \..*?\/ with String#replace to remove anything that is between the . and /.
"example.com/something".match(/\.(.*?)\//)[1] // com
RegEx Explanation:
\.: Match . literal
(.*?): Match anything except newline, non-greedy and add it in first captured group
\/: Match forward slash
something as simple as
var a = "example.com/something"
var c = a.substring( a.indexOf( "." ) + 1, a.indexOf( "/", a.indexOf( "." ) ) );
alert(c);
for replacing,
a = a.replace( c, "NEW" );
alert(a);
to replace it with quotes
a = a.replace( c, "\"\"" );
alert(a);

get particular strings from a text that separated by underscore

I am trying to get the particular strings from the text below :
var str = "001AN_LAST_BRANCH_HYB_1hhhhh5_PBTsd_JENKIN.bin";
From this i have to get the following strings: "LAST", "BRANCH" and "JENKIN".
I used the code below to get "JENKIN";
var result = str.substr(str.lastIndexOf("_") +1);
It will get the result "JENKIN.bin". I need only "JENKIN".
Also the input string str sometimes contains this ".bin" string.
with substring() function you can extract text you need with defining start and end position. You have already found the start position with str.lastIndexOf("_") +1 and adding end position with str.indexOf(".") to substring() function will give you the result you need.
var result = str.substring(str.lastIndexOf("_") +1,str.indexOf("."));
It depends on how predictable the pattern is. How about:
var parts = str.replace(/\..+/, '').split('_');
And then parts[0] is 001AN, parts[1] is LAST, etc
You can use String.prototype.split to split a string into an array by a given separator:
var str = '001AN_LAST_BRANCH_HYB_1hhhhh5_PBTsd_JENKIN.bin';
var parts = str.split('_');
// parts is ['001AN', 'LAST', 'BRANCH', 'HYB', '1hhhhh5', 'PBTsd', 'JENKIN.bin'];
document.body.innerText = parts[1] + ", " + parts[2] + " and " + parts[6].split('.')[0];
You could do that way:
var re = /^[^_]*_([^_]*)_([^_]*)_.*_([^.]*)\..*$/;
var str = "001AN_LAST_BRANCH_HYB_1hhhhh5_PBTsd_JENKIN.bin";
var matches = re.exec(str);
console.log(matches[1]); // LAST
console.log(matches[2]); // BRANCH
console.log(matches[3]); // JENKIN
This way you can reuse your RegExp anytime you want, and it can be used in other languages too.
Try using String.prototype.match() with RegExp /([A-Z])+(?=_B|_H|\.)/g to match any number of uppercase letters followed by "_B" , "_H" or "."
var str = "001AN_LAST_BRANCH_HYB_1hhhhh5_PBTsd_JENKIN.bin";
var res = str.match(/([A-Z])+(?=_B|_H|\.)/g);
console.log(res)
I don't know why you want to that, but this example would be helpful.
It will be better write what exactly you want.
str = '001AN_LAST_BRANCH_HYB_1hhhhh5_PBTsd_JENKIN.bin'
find = ['LAST', 'BRANCH', 'JENKINS']
found = []
for item in find:
if item in str:
found.append(item)
print found # ['LAST', 'BRANCH']

Exclude characters from displaying?

I want to exclude characters from displaying in a vbulletin template.
For example, if a user writes:
"[Hello World] How are you?"
I want to ecxlude "[" and "]" all that's inside so it only displays:
"How are you?"
Is there a way to do this?
Use JavaScript string operations .getIndexOf() and .substring(). Get the position of the first bracket, get the position of the second bracket, split the string into 3 substrings, the middle section being between the two indexed values, and then add just the first and third substrings together. Like this:
var string = "[Hello World] How are you?";
var bracket1 = string.getIndexOf("[");
var bracket2 = string.getIndexOf("]");
var substring1 = string.substring(0,bracket1);
var substring2 = string.substring(bracket1,bracket2);
var substring3 = string.substring(bracket2,string.length);
var solution = substring 1 + " " + substring 3;
At least, that's the concept. Everything may not be right on, but you could futz with the numbers a little to get it perfect.
Or if you don't need to worry about what comes before [], simply use .split():
var string = "[Hello World] How are you?";
var solutionArray = string.split("]");
var solution = solutionArray[1];
Hope this helps!

Categories

Resources