为什么jquery模式匹配方法仅针对第一个元素触发

时间:2015-07-09 11:43:39

标签: jquery pattern-matching expression match

我在此函数中传递字段ID

function expDuration(id) {

     if(id.match(/-exp_dol/g).length > 0 || id.match(/-exp_doj/g).length > 0){
                alert(id);
          }

现在如果我传递了具有模式" -exp_dol"它提醒我身份证 但如果我传递了具有模式的id" -exp_doj"它没有提醒我身份

现在有趣的是在if语句中,如果我先写exp_doj然后是OR运算符然后是exp_dol 像这样

if(id.match(/-exp_doj/g).length > 0 || id.match(/-exp_dol/g).length > 0){
                alert(id);
          }

并传递匹配" -exp_doj"的ID对于" -exp_dol"它会提醒我它没有提醒

所以这是正确的,匹配方法只针对第一个元素而不是针对第二个元素触发,或者我做错了......请建议我

1 个答案:

答案 0 :(得分:-1)

尝试使用,这对我有用

class User < ActiveRecord::Base
  has_many :items
  attr_accessor :remember_token, :activation_token, :reset_token
  before_save   :downcase_email
  before_create :create_activation_digest
  attr_accessor :remember_token
  before_save { self.email = email.downcase }
  validates :username, presence: true, length: { maximum: 50, minimum: 5 }, uniqueness: true
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: true
  has_secure_password
  validates :description, presence: true, length: { maximum: 500 }
  validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
  has_attached_file :avatar, styles: { large: "250x250", medium:"100x100", small:"50x50", thumb:"30x30#"}
  validates_attachment_content_type :avatar, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]


  # Returns the hash digest of the given string.
  def User.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                  BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end

  # Returns a random tocken.
  def User.new_token
    SecureRandom.urlsafe_base64
  end

    # Returns true if a password reset has expired.
  def password_reset_expired?
    reset_sent_at < 2.hours.ago
  end

  # Remembers a user in the database for use in persistent sessions.
  def remember
    self.remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(remember_token))
  end

  # Returns true if the given token matches the digest.
  def authenticated?(attribute, token)
    digest = send("#{attribute}_digest")
    return false if digest.nil?
    BCrypt::Password.new(digest).is_password?(token)
  end

  # Activates an account.
  def activate
    update_attribute(:activated,    true)
    update_attribute(:activated_at, Time.zone.now)
  end

  # Sends activation email.
  def send_activation_email
    UserMailer.account_activation(self).deliver_now
  end

  # Forgets a user.
  def forget
    update_attribute(:remember_digest, nil)
  end

  # Converts email to all lower-case.
  def downcase_email
    self.email = email.downcase
  end

  # Creates and assigns the activation token and digest.
  def create_activation_digest
    self.activation_token  = User.new_token
    self.activation_digest = User.digest(activation_token)
  end

  # Sets the password reset attributes.
  def create_reset_digest
    self.reset_token = User.new_token
    update_attribute(:reset_digest,  User.digest(reset_token))
    update_attribute(:reset_sent_at, Time.zone.now)
  end

  # Sends password reset email.
  def send_password_reset_email
    UserMailer.password_reset(self).deliver_now
  end

end

阅读评论后。这也是可行的。我发布了适用于我的代码。我们可以在没有id="this is -exp_dol sdgfsd"; if(id.match(/(?:-exp_doj|-exp_dol)/ig)) { alert('present'); } 或仅使用ig

的情况下使用此功能
g

这是一个有效的js小提琴jsfiddle