Javascript 如何在字符之前获取术语?
如何获取“x”之前的数字 我尝试使用Javascript 如何在字符之前获取术语?,javascript,regex,Javascript,Regex,如何获取“x”之前的数字 我尝试使用.split('x')[0]但它会捕获“x”之前的所有内容 123x // Gives 123 123y+123x - // Gives 123 123x+123x - // Gives 246 console.log(匹配('123x+123y+456x','x')) console.log(匹配('123x+123y+456x','y')) 函数匹配(str,x){ 返回str.match(新的RegExp('\\d+'+x,'g')).reduce(
.split('x')[0]
但它会捕获“x”之前的所有内容
123x // Gives 123
123y+123x - // Gives 123
123x+123x - // Gives 246
console.log(匹配('123x+123y+456x','x'))
console.log(匹配('123x+123y+456x','y'))
函数匹配(str,x){
返回str.match(新的RegExp('\\d+'+x,'g')).reduce((cur,p)=>{
返回cur+parseInt(p.substr(0,p.length-x.length))
}, 0)
}
console.log(匹配('123x+123y+456x','x'))
console.log(匹配('123x+123y+456x','y'))
函数匹配(str,x){
返回str.match(新的RegExp('\\d+'+x,'g')).reduce((cur,p)=>{
返回cur+parseInt(p.substr(0,p.length-x.length))
}, 0)
}
我已经测试了一个使用regex的函数,我认为它可以工作。我已经包括了结果、函数如何工作的解释,然后是函数的注释版本
注意,这不会处理任何比简单项的加减更复杂的代数。我要提到的是,它是一个可以处理简化的API(我没有附属关系)
结果:
console.log(coefficient("-x+23x")); // 22
console.log(coefficient("123y+123x")); // 123
// replaces spaces
console.log(coefficient("x + 123x")); // 124
console.log(coefficient("-x - 123x")); // -124
console.log(coefficient("1234x-23x")); // 1211
// doesn't account for other letters
console.log(coefficient("-23yx")); // 1
说明:
首先,该函数删除空格。然后它使用一个正则表达式,它可以找到任何后跟“x”的数字序列。如果前面有+/-,正则表达式将保留该值。函数循环遍历这些数字序列,并将它们相加到总数中。如果有一个没有数字的“x”,它的系数被假定为-1或1
注释代码:
function coefficient(str) {
// remove spaces
str = str.replace(/\s/g, '');
// all powerful regex
var regexp = /(\+|-)?[0-9]*x/g
// total
sum = 0;
// find the occurrences of x
var found = true;
while (found) {
match = regexp.exec(str);
if (match == null) {
found = false;
} else {
// treated as +/- 1 if no proceeding number
if (isNaN(parseInt(match[0]))) {
if (match[0].charAt(0) == "-") {
sum--;
} else {
sum++;
}
// parse the proceeding number
} else {
sum += parseInt(match[0]);
}
}
}
return sum;
}
我已经测试了一个使用正则表达式的函数,我认为它可以工作。我已经包括了结果、函数如何工作的解释,然后是函数的注释版本 注意,这不会处理任何比简单项的加减更复杂的代数。我要提到的是,它是一个可以处理简化的API(我没有附属关系) 结果:
console.log(coefficient("-x+23x")); // 22
console.log(coefficient("123y+123x")); // 123
// replaces spaces
console.log(coefficient("x + 123x")); // 124
console.log(coefficient("-x - 123x")); // -124
console.log(coefficient("1234x-23x")); // 1211
// doesn't account for other letters
console.log(coefficient("-23yx")); // 1
说明:
首先,该函数删除空格。然后它使用一个正则表达式,它可以找到任何后跟“x”的数字序列。如果前面有+/-,正则表达式将保留该值。函数循环遍历这些数字序列,并将它们相加到总数中。如果有一个没有数字的“x”,它的系数被假定为-1或1
注释代码:
function coefficient(str) {
// remove spaces
str = str.replace(/\s/g, '');
// all powerful regex
var regexp = /(\+|-)?[0-9]*x/g
// total
sum = 0;
// find the occurrences of x
var found = true;
while (found) {
match = regexp.exec(str);
if (match == null) {
found = false;
} else {
// treated as +/- 1 if no proceeding number
if (isNaN(parseInt(match[0]))) {
if (match[0].charAt(0) == "-") {
sum--;
} else {
sum++;
}
// parse the proceeding number
} else {
sum += parseInt(match[0]);
}
}
}
return sum;
}
我不知道ECMAScript正则表达式中是否有足够的聪明来进行后面的查找,但您可以通过匹配和后处理来删除“x” 如果目的是求和,则需要使用reduce进行进一步操作。修剪x可以与reduce结合使用,这样就不需要贴图了
console.log(
“123x+123x”。匹配(/\d+x/ig).map(函数(v){
返回v.slice(0,-1)
}).reduce(函数(sum,v){return sum++v},0)
);代码>我不知道ECMAScript正则表达式中是否有足够的聪明来进行后面的查找,但您可以通过匹配和后期处理来删除“x”
如果目的是求和,则需要使用reduce进行进一步操作。修剪x可以与reduce结合使用,这样就不需要贴图了
console.log(
“123x+123x”。匹配(/\d+x/ig).map(函数(v){
返回v.slice(0,-1)
}).reduce(函数(sum,v){return sum++v},0)
);
什么是“-1x*200x”
,“10x+30x*2x”
?预期结果是什么?什么是“-1x*200x”
,“10x+30x*2x”
?预期结果是什么?