I want to convert a string such as 'String' to the stripped version of that (I think thats the word?), something like this:
const strip = r => {
/* Code */
}
What I want is:
> strip('String')
> String
basically I just want it to remove the quotes from around a string
(I want the output to be a none-type)
Is this what you are after?
var test = "\"'String \" with 'quotes'\"";
test = test.replace(/['"]/g, "");
console.log("test: " + test);
In your example, the string passed as an argument to the strip function does not have quotes in its content. You're just telling that function that the r parameter is of type string with the content String.
To answer to your question, you can remove the quotes of a string by removing the first and last character:
const strip = str => {
return str.slice(1, -1);
}
strip('"String"') => String
Given this string of codes in a compressed form:
GMZDCOR[R[G[A5D[C,E,K,M,O],E5D[K,M,O],I5D[C,E,K,M,O]],HI[4Q,YT[A,C]]],SHI[2[A,Q],3[A,Q],4A,Y[Q,T[G,I]],Z[A,Q]],THI[2A,3Q,4[A,Q],Y[Q,TA],Z[A,Q]],UHI[YQ,ZA],VHI[2A,3Q,4[A,Q],Y[Q,T[A,C]],Z[A,Q]],WHI[3Q,4A,Y[Q,T[C,E]],ZA],XHI[2[A,Q],ZQ],YHI[2[A,Q],YQ,Z[A,Q]],ZHI[2Q,3[A,Q],YQ,ZA]]
I need it to look like this:
[GMZDCORRGA5DC, GMZDCORRGA5DE, GMZDCORRGA5DK, GMZDCORRGA5DM, ...etc]
Is there a known algorithm for solving this kind of problem? I've tried basic splitting but looks like I need to keep track of all the brackets there.
You can use a regular expression to tokenise the input:
(\w+)([\[]?) to match some alphanumerical string followed by an optional opening bracket, or
\]: to match a closing bracket
Commas don't need to be captured -- they just function as delimiters.
Then use recursion to generate the results for nested expressions.
In JavaScript, this is a good candidate for writing a generator function which returns an iterator over the results:
function parse(s) {
const tokens = s.matchAll(/(\w+)([\[]?)|(\])|$/g);
function* generate(isNested) {
while (true) {
const {value: [all, word, opening, closing]} = tokens.next();
if (isNested ? closing : !all) break;
if (opening) {
for (const substr of generate(true)) {
yield word + substr;
}
} else {
yield word;
}
}
}
return Array.from(generate());
}
const s = "GMZDCOR[R[G[A5D[C,E,K,M,O],E5D[K,M,O],I5D[C,E,K,M,O]],HI[4Q,YT[A,C]]],SHI[2[A,Q],3[A,Q],4A,Y[Q,T[G,I]],Z[A,Q]],THI[2A,3Q,4[A,Q],Y[Q,TA],Z[A,Q]],UHI[YQ,ZA],VHI[2A,3Q,4[A,Q],Y[Q,T[A,C]],Z[A,Q]],WHI[3Q,4A,Y[Q,T[C,E]],ZA],XHI[2[A,Q],ZQ],YHI[2[A,Q],YQ,Z[A,Q]],ZHI[2Q,3[A,Q],YQ,ZA]]";
console.log(parse(s));
I am having issues adding newline characters into a string that already has newlines.
For example:
const foo = "test\ntest";
const string = `mutation {
some_field: "${foo}"
}`;
This will be output as:
mutation {
some_field: "test
test"
}
But I want it to output as:
mutation {
some_field: "test\ntest"
}
Where the existing white-space/newlines are preserved but you can add a string like "test\ntest" inside of it. This current line-breaking is causing syntax errors.
I have tried adding together these strings in various different ways but I can't figure out how to force \n to remain \n in the field value.
When you have \n in a JavaScript string literal, it represents a newline character, not a backslash followed by n. If you want a backslash followed by n, you have to escape the backslash character with another backslash character. That is, you have to put \n in the string literal.
const foo = "test\\ntest";
const string = `mutation {
some_field: "${foo}"
}`;
console.log(string)
You need to put literal \n in the string by escaping the backslash.
const foo = "test\\ntest";
const string = `mutation {
some_field: "${foo}"
}`;
console.log(string);
But this seems like it's fraught with danger. You should almost certainly be using JSON, not a template literal.
const foo = "test\ntest";
const string = `mutation ${JSON.stringify({
some_field: foo
}, null, 4)}`;
console.log(string);
A colleague was attempting to parse a json string generated in another system and ran into behavior we were not able to explain. I've replicated the problem with a very small code example here:
// running in node >= 8 (tried both 8 and 12)
const str1 = '{ "test": "path\\test" }';
const str2 = '{ "test": "path\\atest" }';
try {
console.log('str1:', JSON.parse(str1));
} catch (e) {
console.log('str1 fail')
}
try {
console.log('str2:', JSON.parse(str2))
} catch (e) {
console.log('str2 fail')
}
str1 will successfully parse into the desired format of { test: 'path\test' }
str2 will fail.
It seems like this will only succeed if the character immediately following \\ is one of the valid JavaScript escape characters
(\t: horizontal tab in the str1 case)
The intended behavior is to escape the \\ into a single \
Is there an explanation for this behavior? We're stumped and would appreciate any insight!
When you have a string literal with two backslashes, those two backslashes will be interpreted as a single literal backslash, Eg, the .length of '\\test' is 5, not 6.
JSON.parse only allows backslashes before characters that can be escaped, like t (horizontal tab) and n (newline). When you have a literal backslash before a character that can't be escaped (like a), JSON.parse throws an error. (This is unlike Javascript string literals, which can have unnecessarily escaped normal characters - eg const str = '\a' is equivalent to 'a', and doesn't throw an error.) You can see an illustration of what's permitted in JSON here - as you can see by the fourth graphic, after a \, the only permitted characters are one of "\/bfnrt, or uXXXX, where each X is a hex digit.
If you wanted the value of the test property in the parsed object to be the string path, followed by a literal backslash, followed by test or atest, you'd need to use four backslashes when declaring the string literal - first, to have the Javascript interpreter interpret it as two literal backslashes, and second, to have JSON.parse interpret the two literal backslashes as a single backslash in the parsed string.
const str1 = '{ "test": "path\\\\test" }';
const str2 = '{ "test": "path\\\\atest" }';
try {
console.log('str1:', JSON.parse(str1));
} catch (e) {
console.log('str1 fail')
}
try {
console.log('str2', JSON.parse(str2))
} catch (e) {
console.log('str2 fail')
}
You can also define string literals with String.raw for any single backslash in the string to be interpreted as a single literal backslash (rather than the beginning of an escape sequence):
const str1 = String.raw`{ "test": "path\\test" }`;
const str2 = String.raw`{ "test": "path\\atest" }`;
try {
console.log('str1:', JSON.parse(str1));
} catch (e) {
console.log('str1 fail')
}
try {
console.log('str2', JSON.parse(str2))
} catch (e) {
console.log('str2 fail')
}
I am trying below code:-
var testrename = {
check: function() {
var str = 988,000 PTS;
var test = str.toString().split(/[, ]/);
console.log(test[0] + test[1]);
}
}
testrename.check();
I want output as- 988000
I was trying it on node
Your str variable's assigned value needs to be quoted in order to assign a string value to it, and for it to be recognized as a string.
It looks like what you're trying to do is extract the integer value of a string, so return 988000 from the string "988,000 PTS", and you would use parseInt(string) for that.
Update: The comma will break the parseInt function and return a truncated number, (988 not 988000) so you can use the replace function with a regular expression to remove all non-numeric values from the string first.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/parseInt
var testrename={
check :function() {
var str ="988,000 PTS";
cleanStr = str.replace(/\D/g,'');
var test = parseInt(cleanStr);
console.log(test);
}
} testrename.check();