简体   繁体   English

使用jQuery / Javascript的正则表达式

[英]Regular expression using jQuery/Javascript

I am writing a script (where on click) will show the output of the checkbox that has been clicked. 我正在编写一个脚本(在单击时),将显示已单击复选框的输出。 So far this is working correctly. 到目前为止,这工作正常。 the output is the label of the checkbox that has been checked, for example, hello 123, 输出是已选中的复选框的标签,例如hello 123,

I would like the output to show just the words and not the numbers. 我希望输出仅显示单词而不显示数字。 I know this would be done with a regular expression, but have never used regular expression in javascript before, and not sure how to go about it. 我知道可以使用正则表达式来完成此操作,但是以前从未在javascript中使用过正则表达式,并且不确定如何去做。 Any help would be grateful 任何帮助将不胜感激

jQuery('#container').on('click', '#inputID', function() {
  if (jQuery(this).is(':checked')) {
    console.debug('checked:', jQuery(this).next().text()); 
  } else { 
    console.debug('unchecked:', jQuery(this).next().text());
  };
});

Thanks 谢谢

You may use match function. 您可以使用match功能。

console.debug('checked:', jQuery(this).next().text().match(/[a-z]+/i)[0]);

[az]+ would match one or more letters. [az]+将匹配一个或多个字母。 i modifier helps to do a case-insensitive match. i修饰符有助于进行不区分大小写的匹配。 By default match returns the output as array. 默认情况下match将输出作为数组返回。 So by accessing the index 0 will give you the first elemnt. 因此,通过访问索引0将给您第一个元素。

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

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