字符串 ‘Regex’ 针对以下情况的验证

huangapple go评论82阅读模式
英文:

String 'Regex' Validation for below scenario

问题

// 我想验证表达式字符串是否符合以下情况
// 1. 表达式应使用 variables 数组的值
// 2. 表达式不应包含除允许的数学函数之外的内容。

// 我尝试了下面的代码
var variables = ['A','B'];
var allowedMathFunc = ['Sin','Cos','Tan'];
var expression= 'Sin(B)+Tan(B)+100+acos(A)';

// 对于变量检查,我尝试了这个
let expressionVariables = expression.replace(/Sin|Log|Exp|Tan|Pos|Rnd|[^A-Za-z]/ig,"");
let expressionUnUsedVar= variables.filter(v=> !expressionVariables.includes(v));
英文:
var variables = ['A','B'];
var allowedMathFunc = ['Sin','Cos','Tan']
var expression= 'Sin(B)+Tan(B)+100+acos(A)+C'

I want to validate expression string match below scenario

  1. expression should use variables array values
  2. expression should not contain other then allowed math functions.

I tried below code

var variables = ['A','B'];
var allowedMathFunc = ['Sin','Cos','Tan']
var expression= 'Sin(B)+Tan(B)+100+acos(A)'

for variable check I tried this 
let expressionVariables = value.replace(/Sin|Log|Exp|Tan|Pos|Rnd|[^A-Za-z]/ig,"");
let expressionUnUsedVar= variables.filter(v=> !expressionVariables.includes(v));

I don't know how to write for both scenario for regex it's not a problem for two different regex.

答案1

得分: 1

你可以使用正则表达式提取所有变量和函数名称,然后检查它们是否包含在允许的函数和变量列表中(区分大小写):

function testExpression(expression, vars, funcs) {
  const usedVariables = expression.match(/\b[a-z]+\b(?!\s*\()/gi) || [];
  const usedFunctions = expression.match(/\b[a-z]+\b(?=\s*\()/gi) || [];
  return usedVariables.every(v => vars.includes(v)) && usedFunctions.every(func => funcs.includes(func));
}

var variables = ['A', 'B'];
var allowedMathFunc = ['Sin', 'Cos', 'Tan'];
var expression = 'Sin(B)+Tan(B)+100+acos(A)+C';
console.log(testExpression(expression, variables, allowedMathFunc));

console.log(testExpression('Sin(B)+Tan(B)+100+Cos(A)+C', ['A', 'B', 'C'], ['Sin', 'Cos', 'Tan']));

希望这有所帮助。

英文:

You can use regex to extract all variables and function names, and then check them if they're included in the allowed function and variable lists (case sensitive):

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function testExpression(expression, vars, funcs) {
  const usedVariables = expression.match(/\b[a-z]+\b(?!\s*\()/gi) || [];
  const usedFunctions = expression.match(/\b[a-z]+\b(?=\s*\()/gi) || [];
  return usedVariables.every(v =&gt; vars.includes(v)) &amp;&amp; usedFunctions.every(func =&gt; funcs.includes(func));
}

var variables = [&#39;A&#39;,&#39;B&#39;];
var allowedMathFunc = [&#39;Sin&#39;,&#39;Cos&#39;,&#39;Tan&#39;]
var expression= &#39;Sin(B)+Tan(B)+100+acos(A)+C&#39;
console.log(testExpression(expression, variables, allowedMathFunc));

console.log(testExpression(&#39;Sin(B)+Tan(B)+100+Cos(A)+C&#39;, [&#39;A&#39;,&#39;B&#39;,&#39;C&#39;], [&#39;Sin&#39;,&#39;Cos&#39;,&#39;Tan&#39;]));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月16日 15:34:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定