我创建了一个方程求解程序。在我的最后一步,我得到了一些错误。如下所示:-
$(document).ready(function () {
$('.solve').click(function () {
var str1 = $('#equ').val();
var select = $('#selected').text();// x value
var fnd = str1.split(new RegExp(select,"g")); // x value split
var fnd_demo = $('#demo').html('without x='+fnd);// without x
var final = fnd_demo.replace(/([-+])?s*(d+)?([a-z])/g, '');//replace all alpha numeric into space
$('#demo1').text('only numeric'+final);//display the numeric only(error:not show any result) applied with 'text('work')' this also not work
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input type="text" placeholder="Enter equation" value="10x+10z-108k+10y-10x+50" class="equ" id="equ">
<input type="button" value="solve" class="solve" id="solve" onclick="solve()">
<p id="selected">x</p>
<p id="demo"></p>
<p id="demo1"></p>
x
值从某个选定的id中获取,并用新的Regexp应用于正则表达式,得到一些结果。结果是,用空格替换时不起作用,用一些文本打印时也不起作用。demo1 not working.
原因是什么并更正我的代码。我期望的答案只有这样的数值:10,-10,+50
谢谢
认为你想要这样的东西,
> var s = '10x+10z-108k+10y-10x+50'
> s.match(/[+-]?d+(?=x|$)/g)
[ '10', '-10', '+50' ]
或
> var v = 'x';
> s.match(RegExp("[+-]?\d+(?=" + v + "|$)", "g"))
[ '10', '-10', '+50' ]