在我的等式中,例如:-10x+10y+5y+22y-10x//x,y,y,y,x
来自字符串匹配函数。我需要像 x 这样的答案,y
。表示多值匹配字符串返回单值。请给出建议。谢谢
var str ='10x+10y+10x-10y'
document.write(str.match(/[a-z]/g));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
查看答案 x 和 y 是双重时间存在的。我需要 x,y
..如果方程中存在多个变量,则仅返回单个变量
最佳答案
怎么样
var allVars='10x+10y+5y+22y-10x'.match(/[a-z]/g),
uniqueVars = [];
$.each(allVars, function(i, el){
if($.inArray(el, uniqueVars) === -1) uniqueVars.push(el);
});
document.write(uniqueVars.join());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
关于javascript - 如何使用正则表达式匹配多个值返回单个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37209111/