简体   繁体   中英

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,

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. 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.

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

[az]+ would match one or more letters. i modifier helps to do a case-insensitive match. By default match returns the output as array. So by accessing the index 0 will give you the first elemnt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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