Regex for split () in javascript

2

I'm trying to make a pattern that separates a text if it finds one of the following characters: +, -, x, /,.
I tried with the following code but it does not work, could someone help me?

var texto = "Hol+A q-ue txal e/st.as";
var separador = texto.split(/+|-|x|\//);
console.log(separador)
    
asked by Vlad T 25.10.2018 в 13:06
source

1 answer

7

Indeed, the regex you are using is not the correct one. You must use the following:

/[+x\-.\/]/

You were missing the brackets to indicate the range of characters to search, and inside the brackets you do not need to use | . It was also unnecessary to escape / , and yet if it was escape - . Finally, you were missing the . in your regular expression.

Snippet:

var texto = "Hol+A q-ue txal e/st.as";
var separador = texto.split(/[+x\-./]/);
console.log(separador)
    
answered by 25.10.2018 / 13:15
source