新的正则表达式调用

时间:2016-05-09 05:19:45

标签: jquery regex string replace mat

我创建了一个方程求解程序。在最后一步中我得到了一些错误。如下所示: -



$(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>
&#13;
&#13;
&#13;

x从一些选定的id获取值,并使用新的Regexp应用于正则表达式并获得一些结果。结果是替换为空格不适用于当时应用某些文本来打印那个&#39;也没有用。demo1 not working.什么是原因并纠正我的代码。我期望的答案只是这样的数值:10,-10,+50谢谢

1 个答案:

答案 0 :(得分:1)

认为你想要这样的东西,

> 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' ]