如何在价值之前只获得一个定界符。不是所有人
how to get only a delimiter before value .not for all
我有一个分隔符拆分功能
应用此代码时,所有值都存在。我只需要像(-5,-7,89)这样的定界符之前的值。我如何获得它们?
function solve() {
str1 = $('#equ').val();
var eql = str1.split(/x/g);
$('#test').html(eql);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" placeholder="Enter equation" value="-5x-7x-56+89x=108" class="equ" id="equ">
<input type="submit" value="solve" class="solve" id="solve" onclick="solve()">
<p id="test"></p>
eql[0] 将为您提供前定界符的值。
$('#test').html(eql[0]);
我不确定我是否完全理解您的问题,但您可能希望获得可选 -
加上出现在 x
之前的数字的所有匹配项。它看起来像这样:
var matches = str1.match(/-?\d+(?=x)/g);
console.log(matches); // You will get an array with [-5, -7, 89]
// For example, we can now output the values
$('#test').text(matches.join(", "));
简短说明:.match
returns 是某个 regular expression 的匹配数组。 -?
匹配一个可选的减号,\d+
匹配一个或多个数字,(?=x)
检查下一个字符是否为 x
而不将其包含在匹配中。最后,正则表达式末尾的 g
修饰符使其成为全局正则表达式,这将使 .match
return 字符串的所有部分都匹配此正则表达式(与 return 相反ing 一场比赛加上所有子比赛,如果有的话)。
我有一个分隔符拆分功能
应用此代码时,所有值都存在。我只需要像(-5,-7,89)这样的定界符之前的值。我如何获得它们?
function solve() {
str1 = $('#equ').val();
var eql = str1.split(/x/g);
$('#test').html(eql);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" placeholder="Enter equation" value="-5x-7x-56+89x=108" class="equ" id="equ">
<input type="submit" value="solve" class="solve" id="solve" onclick="solve()">
<p id="test"></p>
eql[0] 将为您提供前定界符的值。
$('#test').html(eql[0]);
我不确定我是否完全理解您的问题,但您可能希望获得可选 -
加上出现在 x
之前的数字的所有匹配项。它看起来像这样:
var matches = str1.match(/-?\d+(?=x)/g);
console.log(matches); // You will get an array with [-5, -7, 89]
// For example, we can now output the values
$('#test').text(matches.join(", "));
简短说明:.match
returns 是某个 regular expression 的匹配数组。 -?
匹配一个可选的减号,\d+
匹配一个或多个数字,(?=x)
检查下一个字符是否为 x
而不将其包含在匹配中。最后,正则表达式末尾的 g
修饰符使其成为全局正则表达式,这将使 .match
return 字符串的所有部分都匹配此正则表达式(与 return 相反ing 一场比赛加上所有子比赛,如果有的话)。