Jquery 如果字符串包含0或负值,请更改文本

Jquery 如果字符串包含0或负值,请更改文本,jquery,Jquery,我想根据数字修改文本,但结果总是给我错误 jQuery(文档).ready(函数($){ var matches=$(“.content”).text().match(/\d/); 如果(匹配){ $(“.result”).text('有一个数字!'); }else if(matches==0 | | matchesjQuery(document).ready(函数($){ var matches=$(“.content”).text().match(/\d/); 警报(“匹配-->”+匹配)

我想根据数字修改文本,但结果总是给我错误

jQuery(文档).ready(函数($){
var matches=$(“.content”).text().match(/\d/);
如果(匹配){
$(“.result”).text('有一个数字!');
}else if(matches==0 | | matches
jQuery(document).ready(函数($){
var matches=$(“.content”).text().match(/\d/);
警报(“匹配-->”+匹配);
如果(匹配项>0){
$(“.result”).text('有一个数字!');

}else if(matches==0 | | matches问题在于
if
语句的顺序

试试这个

jQuery(document).ready(function($) {

  var matches = parseInt($(".content").text().match(/-?\d+/g));
  if (matches === 0 || matches <= 0) {
    $(".result").text('Number cannot be negative or 0.');
  } else if (matches) {
    $(".result").text('There is a Number!');
  } else {
    $(".result").text('There is no Number!');
  }

});
jQuery(文档).ready(函数($){
var matches=parseInt($(“.content”).text().match(/-?\d+/g));

如果(matches===0 | | matches非常感谢,但是如果有负值,它应该返回“Number不能是负数或0”。但是现在是“there as Number!”@user9030616问题在于您的正则表达式。它只接受数字(所以没有否定词。我更新了答案。@VVV,else案例在您的JSFIDLE示例中似乎不起作用。非常感谢,但是负值应该返回“Number不能是负数或0”。^-\d+$这是负数的正则表达式。^-?\d*\.{0,1}\d+$是正数和负数的正则表达式
jQuery( document ).ready(function($) {

  var matches = $(".content").text().match(/\d/);
  alert("matches -->" + matches);

  if (matches > 0){
     $(".result").text('There is a Number!');    
  } else if (matches == 0 || matches <= 0) {
     $(".result").text('Number cannot be negative or 0.');    
  } else {
     $(".result").text('There is no Number!');
  }

});
jQuery(document).ready(function($) {

  var matches = parseInt($(".content").text().match(/-?\d+/g));
  if (matches === 0 || matches <= 0) {
    $(".result").text('Number cannot be negative or 0.');
  } else if (matches) {
    $(".result").text('There is a Number!');
  } else {
    $(".result").text('There is no Number!');
  }

});