js USES the split function to split a string by multiple characters

  • 2020-05-17 04:45:25
  • OfStack

This example shows how js USES the split function to split a string by multiple characters. Share with you for your reference. The specific analysis is as follows:

js split in () function can be carried out in accordance with the specified symbol string segmentation, but if there are multiple segmentation symbols in the string, the js split whether () function can also be competent, the answer is yes, the js split () function can be realized through the regular expression delimiters string segmentation, call is simple, the following is a detailed example.

The following code separates strings by commas using the js split method


var mystring = "a,b,c,d,e";
var myarray = mystring.split(",");

If there is such a string: "ofstack. com, google. com, baidu com_weibo. com_haotu. net",
We want to separate the website address by both comma and underscore. Please refer to the following code:


var mystring = "ofstack.com,google.com,baidu.com_weibo.com_haotu.net";
var myarray = mystring.split(/[,_]/);

I hope this article is helpful for you to design javascript program.


Related articles: