Regexp 字符串匹配函数在新的 Regex 调用后不起作用
Regexp string match function not working after new Regex call
我已经创建了一个方程求解器 program.In 我的最后一步得到了一些 error.is 如下所示:-
$(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 应用于正则表达式并获得一些 result.That 结果被替换为 space 在应用某些时不起作用要打印的文本也不起作用。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' ]
我已经创建了一个方程求解器 program.In 我的最后一步得到了一些 error.is 如下所示:-
$(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 应用于正则表达式并获得一些 result.That 结果被替换为 space 在应用某些时不起作用要打印的文本也不起作用。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' ]