0

I am trying to make something similar to the way that jQuery uses strings to determine HTML elements. I tried using a split function to make it become a list of the different values but instead it returned an array with 20 empty strings.

console.log('button#hello.something'.split(/.|#/))

And the result is:

[
  '', '', '', '', '', '', '', '',
  '', '', '', '', '', '', '', '',
  '', '', '', '', '', '', ''     
]

Barmar
  • 741,623
  • 53
  • 500
  • 612
Hex
  • 59
  • 1
  • 12

2 Answers2

3

. means any single charactar in regex, you need to skip it using a backslash:

console.log('button#hello.something'.split(/\.|#/))
Amir MB
  • 3,233
  • 2
  • 10
  • 16
0

Your regex is wrong if you want to match exactly dots and sharps. You need to evade both like so /\.|\#/ Dots in regexp matches any character, so your split function effectively splits your string by any character.

CrimsonFreak
  • 127
  • 7
  • 1
    `#` shouldn't need escaping ~ [What special characters must be escaped in regular expressions?](https://stackoverflow.com/q/399078/283366) – Phil Dec 07 '22 at 23:48