javascript - 如何使用正则表达式匹配多个值返回单个值?

标签 javascript jquery regex string match

在我的等式中,例如:-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/

相关文章:

使用 Fancybox 进行 jQuery 同位素过滤

javascript - wordpress jquery 插件并不是一个函数

c# - REGEX 删除被 < pikes > 包围的字符串

regex - VBA - 带有变量的正则表达式拆分

javascript - 我似乎无法让 json 工作。句号。我几乎复制了 http ://api. jquery.com/jQuery.getJSON/但没有运气

Javascript自定义排序

javascript - 使用 xmlhttprequest 获取跨域 PNG 文件

javascript - 用于输入 val 的 jQuery 对话框脚本

javascript - 更改location.href后执行函数

正则表达式 : Validate phone number