1

EDIT: This string is a part of json and i am writing it onto a file. In that json file i see the escape charaters. With console.log, I don't see any escape character.

I am trying to concatenate a string such that I get the following output:

"OR("1admin", "2admin")"

But the output I keep on getting is

"OR(\"1admin\", \"2admin\")"

Sample code

var str = "OR("
for (let i = 1; i < 3; i++) {
  str += '"' + i + 'admin' + '", ';
}
str = str.trim().substring(0, str.length - 2).concat(')')
console.log(str)

I have tried using regex and string split. eg.

.replace(/'/g, '"') This when i tried something like this "'" + i + "admin" + "', " and tried to replace ' with "

.split('\\').join('').trim() This also didn' work.

What am I doing wrong?

18
  • 8
    "OR("1admin", "2admin")" is not a syntactically correct string. The second one on the other hand is a string with the character sequence you want: i.e. 'O', 'R', '(', '"', ... . In other words, you already got the right result. The string "\"" is the string consisting of the character '"'. A backlash character '\' is required to escape the '"' character so that it doesn't break string parsing early. Commented May 29, 2019 at 13:41
  • 2
    @fk82 that would be a good answer :) Commented May 29, 2019 at 13:44
  • See escape notation and Single quotes vs. double quotes | your output is actually correct... Commented May 29, 2019 at 13:45
  • @JonasWilms Technically I'm not answering the question. Let's see what OP says. :) Commented May 29, 2019 at 13:46
  • 2
    Without the \ it would be invalid JSON then. Commented May 29, 2019 at 13:53

3 Answers 3

2

As mentioned, you already got the correct result in your original snippet.

If the character escape syntax seems curious to you, I'd recommend something like the following using template strings.

You can write the " in template strings unescaped and use interpolation to print your variables.

Edit: Seems like you want JSON formatting. You can use JSON.stringify for that. JSON formatted strings will contain escape characters that show up in console.log output.

const f = (n, s) => {
    const a = new Array(n)
      .fill()
      .map((_, i) => `"${i + 1}${s}"`);
    return `OR(${a.join(', ')})`;
}

console.log(`output: ${f(2, "admin")}`)
console.log(`json-formatted: ${JSON.stringify(f(2, "admin"))}`)

Sign up to request clarification or add additional context in comments.

Comments

0

This seems to do the job:

const NUM_ADMINS = 2;
const CONCAT_STRING =
 `OR(${
   Array(NUM_ADMINS)
   .fill()
   .map((_, idx) => `"${idx + 1}admin"`)
   .join(", ")})`;

It creates a new array with the needed number of admins, then maps it to strings in th form "admin", joins with a comma and a space. And then it it is all wrapped in a "OR()" string.

Comments

0

var str = '"OR("'
var adm = 'admin"'
var end = ')"'
for (let i = 1; i < 3; i++) {
  str += i + adm + ', '
}
str = str.trim().substring(0, str.length - 2)
str += end
console.log(str);

try running this snippet. if the implementation is fyn for you. Result is what you expected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.