Advertisement
Web Developer Resources
You Are Here: ESQSoft.com ¬ javascript_examples ¬ JavaScript Regular Expression (REGEX) Example
E.S.Q. Software Logo

JavaScript Regular Expression (REGEX) Example

How to find an underlined letter (or character) using JavaScript and Regular Expressions (regex or regexp). One great convenience for folks who do data entry is shortcut keys. Interface designers will sometimes build underlines into their designs to imply short-cut keys. These can be built into form inputs using the ACCESSKEY property of inputs. Since I can't always control whether the HTML author includes a specific letter, or even any underlined letters, it's nice to be able to dynamically ask for them and include that dynamic code as a library component so its always present. Here's a JavaScript implementation of a RegExp to get a single HTML underlined letter within an object (a button in this case). <button onclick="alert(checkId(this.innerHTML))">This is a te<u>s</u>t</button>

(Clicking the button below show alert the underlined character(s))

  
What about more than one underline? The script can find these as well.

Here's the function the buttons are calling. This contains a simple regex pattern to look for HTML underlines: <script> function checkIt(val){ var str = 'Results:\n'; var x = val.match(/<u>[a-zA-Z]*<\/u>/gim); if(x){ str += 'Found: '+x.toString().replace(/<[^>]+>/gim,''); } else{ str += 'No underlines were found.'; } return str; } </script>
Advertisement
Advertisement
Statcounter