admin管理员组

文章数量:1401361

In php I can do this to place a back slash inside an array

$symbols = array(".","\\\\");

What is the best method of doing this in javascript with backslash, colon, and semicolon?

In php I can do this to place a back slash inside an array

$symbols = array(".","\\\\");

What is the best method of doing this in javascript with backslash, colon, and semicolon?

Share Improve this question edited Jul 19, 2020 at 1:09 Unmitigated 89.8k12 gold badges99 silver badges104 bronze badges asked Jul 19, 2020 at 0:50 JonnyJonny 1,3291 gold badge15 silver badges27 bronze badges 2
  • 1 Inside a string value, correct? PHP and JavaScript share the same rules for backslash-escaped characters in strings which they inherited from C. – Dai Commented Jul 19, 2020 at 0:53
  • yes inside of an array – Jonny Commented Jul 19, 2020 at 0:54
Add a ment  | 

2 Answers 2

Reset to default 6

Use an array literal delimited by square brackets:

var symbols = [".","\\", ":", ";"];
console.log(symbols);

See here for a more thorough explanation of arrays and their methods.

you need to scape backslash (backslash is the escape keyword). If you need to push an item to the array use the push() method.

let arr = ['\\', ':'];
arr.push(';');

console.log(arr);   //Outputs ["\", ":", ";"]

本文标签: How can i place special characters in an array in javascriptStack Overflow