简体   繁体   English

如何在 JavaScript 中的正则表达式中精确匹配 0.00

[英]How to match exactly 0.00 in regular expression in JavaScript

I'm using DataTables to handle tables and I made a filters for columns that contain money data.我正在使用 DataTables 来处理表格,并为包含货币数据的列制作了一个过滤器。 Each filter is a select with two options, "Have" and "Not Have", where "Have" is when data in each cell of the column is different of 0.00 and "Not Have" is the opposite.每个过滤器都是一个 select 有两个选项,“有”和“没有”,其中“有”是当列的每个单元格中的数据不同时为 0.00,而“没有”则相反。

I made an example here:我在这里做了一个例子:

 $(document).ready(function() { var table = $('#example').DataTable(); $('#select-input').on('keyup change', function() { var item = $(this) if ($(this).find('option:selected').val() === "1") { table.column(5).search('^((?.0.00),)*$'. true);draw(). } else if ($(this):find('option.selected').val() === "0") { table.column(5).search('0.00');draw(). } else { table.search('').columns().search('');draw(); } }) });
 body { font: 90%/1.45em "Helvetica Neue", HelveticaNeue, Verdana, Arial, Helvetica, sans-serif; margin: 0; padding: 0; color: #333; background-color: #fff; }
 <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script> <link href="https://nightly.datatables.net/css/jquery.dataTables.css" rel="stylesheet" type="text/css" /> <script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script> <div class="container"> <select id="select-input"> <option value="">Select</option> <option value="1">Have</option> <option value="0">Not Have</option> </select> <table id="example" class="display nowrap" width="100%"> <thead> <tr> <th>Name</th> <th>Position</th> <th>Office</th> <th>Age</th> <th>Start date</th> <th>Salary</th> </tr> </thead> <tfoot> <tr> <th>Name</th> <th>Position</th> <th>Office</th> <th>Age</th> <th>Start date</th> <th>Salary</th> </tr> </tfoot> <tbody> <tr> <td>Tiger Nixon</td> <td>System Architect</td> <td>Edinburgh</td> <td>61</td> <td>2011/04/25</td> <td>160.00</td> </tr> <tr> <td>Garrett Winters</td> <td>Director</td> <td>Edinburgh</td> <td>63</td> <td>2011/07/25</td> <td>30.00</td> </tr> <tr> <td>Ashton Cox</td> <td>Technical Author</td> <td>San Francisco</td> <td>66</td> <td>2009/01/12</td> <td>30.00</td> </tr> <tr> <td>Cedric Kelly</td> <td>Javascript Developer</td> <td>Edinburgh</td> <td>22</td> <td>2012/03/29</td> <td>30.00</td> </tr> <tr> <td>Jenna Elliott</td> <td>Financial Controller</td> <td>Edinburgh</td> <td>33</td> <td>2008/11/28</td> <td>30.00</td> </tr> <tr> <td>Brielle Williamson</td> <td>Integration Specialist</td> <td>New York</td> <td>61</td> <td>2012/12/02</td> <td>30.00</td> </tr> <tr> <td>Herrod Chandler</td> <td>Sales Assistant</td> <td>San Francisco</td> <td>59</td> <td>2012/08/06</td> <td>30.00</td> </tr> <tr> <td>Rhona Davidson</td> <td>Integration Specialist</td> <td>Edinburgh</td> <td>55</td> <td>2010/10/14</td> <td>31.00</td> </tr> <tr> <td>Colleen Hurst</td> <td>Javascript Developer</td> <td>San Francisco</td> <td>39</td> <td>2009/09/15</td> <td>31.00</td> </tr> <tr> <td>Sonya Frost</td> <td>Software Engineer</td> <td>Edinburgh</td> <td>23</td> <td>2008/12/13</td> <td>31.00</td> </tr> <tr> <td>Jena Gaines</td> <td>System Architect</td> <td>London</td> <td>30</td> <td>2008/12/19</td> <td>31.00</td> </tr> <tr> <td>Quinn Flynn</td> <td>Financial Controller</td> <td>Edinburgh</td> <td>22</td> <td>2013/03/03</td> <td>42.00</td> </tr> <tr> <td>Charde Marshall</td> <td>Regional Director</td> <td>San Francisco</td> <td>36</td> <td>2008/10/16</td> <td>53.00</td> </tr> <tr> <td>Haley Kennedy</td> <td>Senior Marketing Designer</td> <td>London</td> <td>43</td> <td>2012/12/18</td> <td>48.00</td> </tr> <tr> <td>Tatyana Fitzpatrick</td> <td>Regional Director</td> <td>London</td> <td>19</td> <td>2010/03/17</td> <td>28.75</td> </tr> <tr> <td>Michael Silva</td> <td>Senior Marketing Designer</td> <td>London</td> <td>66</td> <td>2012/11/27</td> <td>37.50</td> </tr> <tr> <td>Paul Byrd</td> <td>Javascript Developer</td> <td>New York</td> <td>64</td> <td>2010/06/09</td> <td>0.00</td> </tr> </tbody> </table> </div>

The problem is when selected option is "Not Have", numbers like 160.00 are been included in the result, and if option is "Have" the result is the opposite. The problem is when selected option is "Not Have", numbers like 160.00 are been included in the result, and if option is "Have" the result is the opposite. What can I do?我能做些什么?

 var x="160.00"; var y="0.00" var z="0.001" console.log("-------------with match--------------") console.log(x.match(/^(0.00$).*/)?true:false) console.log(y.match(/^(0.00$).*/)? true:false) console.log(z.match(/^(0.00$).*/)?true:false) console.log("-------------with search--------------") console.log(x.search(/^(0.00$).*/)>=0) console.log(y.search(/^(0.00$).*/)>=0) console.log(z.search(/^(0.00$).*/)>=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM