简体   繁体   English

如何在字符串中查找特殊字符并将其存储在JavaScript中的数组中

[英]How to find special characters in a string and store in an array in javascript

Example of a string 字符串示例

"/city=<A>/state=<B>/sub_div=<C>/type=pos/div=<D>/cli_name=Cstate<E>/<F>/<G>"

characters occurs like A, B, C and .... are variables and count is not fixed 出现像A,B,C和...这样的字符是变量,并且计数不固定

How to identifies how many variables are there and stored in an array 如何识别有多少变量并存储在数组中

Use regex to find all your matches. 使用正则表达式查找所有匹配项。 Using a while loop you can iterate through multiple matches and push them in an array. 使用while循环,您可以遍历多个匹配项并将它们推入数组。 Try this. 尝试这个。

var String = "/city=<A>/state=<B>/sub_div=<C>/type=pos/div=<D>/cli_name=Cstate<E>/<F>/<G>";
var myRegexp = /\<.\>/gm;
var matches = [];
var match = myRegexp.exec(String);

while (match != null) {  
  matches.push(match[0])
  match = myRegexp.exec(String);
}

console.log(matches)

Please review below code that will help to resolve your issue. 请查看下面的代码,这将有助于解决您的问题。 It may find any non word characters and create a non-word array. 它可能会找到任何非单词字符并创建一个非单词数组。

 let str = "/city=<A>/state=<B>/sub_div=<C>/type=pos/div=<D>/cli_name=Cstate<E>/<F>/<G>"; let arrStr = str.split(""); var strRegExp = /\\W/g; let arrNonWord = []; arrStr.forEach(function(str){ var result = str.match(strRegExp); if(result) arrNonWord.push(result[0]); }); console.log(arrNonWord); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM