Skip to content
Fix Code Error

Check whether a string matches a regex in JS

March 13, 2021 by Code Error
Posted By: Anonymous

I want to use JavaScript (can be with jQuery) to do some client-side validation to check whether a string matches the regex:

^([a-z0-9]{5,})$

Ideally it would be an expression that returned true or false.

I’m a JavaScript newbie, does match() do what I need? It seems to check whether part of a string matches a regex, not the whole thing.

Solution

Use regex.test() if all you want is a boolean result:

console.log(/^([a-z0-9]{5,})$/.test('abc1')); // false

console.log(/^([a-z0-9]{5,})$/.test('abc12')); // true

console.log(/^([a-z0-9]{5,})$/.test('abc123')); // true

…and you could remove the () from your regexp since you’ve no need for a capture.

Answered By: Anonymous

Related Articles

  • Reference - What does this regex mean?
  • Get next available date for an out-of-stock, but…
  • Aurelia bundling issue with virtual directory
  • AppCompat v7 r21 returning error in values.xml?
  • Apache server keeps crashing, "caught SIGTERM,…
  • How to use aurelia-validate with a object properties…
  • How do you access the matched groups in a JavaScript…
  • Using Aurelia validation in model
  • How to filter a RecyclerView with a SearchView
  • Centralize Aurelia validation logic
  • regex match any single character (one character only)
  • What are the undocumented features and limitations…
  • Spring MVC: How to perform validation?
  • How to use Regular Expressions (Regex) in Microsoft…
  • Aurelia validation working but not displaying…
  • Aurelia Validation rule (bound to model) does not…
  • Is there some way to test my grammar which refer to…
  • "Thinking in AngularJS" if I have a jQuery background?
  • Regular expression to match numbers with or without…
  • aurelia-validation: validation errors are not shown…
  • Aurelia Validation - no message shown on failed validation
  • Unable to inject aurelia-validation
  • How to change the color of vaadin-select-text-field…
  • Latest jQuery version on Google's CDN
  • What's the difference between eval, exec, and compile?
  • How to check if a string contains text from an array…
  • regex.test V.S. string.match to know if a string…
  • How to grep (search) committed code in the Git history
  • Overriding .fetch() works with fake data, errors…
  • Regex negative lookahead is not working properly
  • jQuery Mobile: document ready vs. page events
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • What does this symbol mean in JavaScript?
  • ASP.NET Custom Validator Client side & Server…
  • How to capture a word boundary in Swift NSRegularExpression?
  • How to set HTML5 required attribute in Javascript?
  • Dynamically color multiple Shiny sliders?
  • shell script. how to extract string using regular…
  • What is the difference between re.search and re.match?
  • Flex: REJECT rejects one character at a time?
  • How to check if a string contains only digits in Java
  • Remove accents/diacritics in a string in JavaScript
  • Regex to Match Symbols: !$%^&*()_+|~-=`{}[]:";'?,./
  • Using Aurelia validation addObject and removeObject…
  • ember.js, ember-cli: Outlets not nesting properly
  • Regex to match only uppercase "words" with some exceptions
  • How to find out client ID of component for ajax…
  • Kodein + Ktor = mutation attempt of frozen…
  • Accessibility and all these JavaScript frameworks
  • What's is the difference between train, validation…
  • Python regex expression between variable strings and…
  • Validation and inheritence
  • How to write a custom ValidationRule
  • JavaScript: client-side vs. server-side validation
  • Aurelia: Trigger Validation on tab-out(blur event)
  • Downloading jQuery UI CSS from Google's CDN
  • How do I return the response from an asynchronous call?
  • Filtering an object based on key, then constructing…
  • Aurelia Validation validation error detected, but no…
  • How to compare Boolean?
  • Is it possible to apply CSS to half of a character?
  • au build complains about file not found or accessible
  • MQTT paho - no error when failed to publish message
  • The definitive guide to form-based website authentication
  • Uncaught (in promise) TypeError: states.filter is…
  • Aurelia Validation - Form is loaded with data from a…
  • TypeError: Cannot read property 'webpackJsonp' of undefined
  • Resetting aurelia-validation after a successful submit
  • Email and phone Number Validation in android
  • Use ASP.NET MVC validation with jquery ajax?
  • Getting the closest string match
  • Use dynamic (variable) string as regex pattern in JavaScript
  • Validation works first time, but not subsequently
  • VeeValidate v3: How to handle backend validation messages?
  • How can I compose a VM into a view within an Aurelia…
  • What is your most productive shortcut with Vim?
  • How to use Servlets and Ajax?
  • Adding Regex to a Vue.js Data Object
  • Does it make sense to integrate backbone.js with ASPNET MVC?
  • Aurelia: validating form with reusable validatable…
  • React-router urls don't work when refreshing or…
  • Java HTTPS client certificate authentication
  • How to load aurelia-validation plugin in Karma test…
  • Adversarial input for this regex
  • RegEx match open tags except XHTML self-contained tags
  • How to create websockets server in PHP
  • Why the REPL is not showing the full trace of the…
  • Using RegEx in SQL Server
  • Create custom FluentRule for aurelia validation
  • Managing jQuery plugin dependency in webpack
  • login page in aurelia
  • Javax.net.ssl.SSLHandshakeException:…
  • Aurelia Validation - What is the best way to access…
  • Custom validator for backbone.js validation
  • How to add custom validation to an AngularJS form?
  • Validation does not work for divs that are initially hidden
  • Could not load file or assembly 'Newtonsoft.Json' or…
  • How to secure EmberJS or any Javascript MVC framework?
  • Displaying a webcam feed using OpenCV and Python
  • Inserting the succeeded word before 'and'…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

jQuery hasAttr checking to see if there is an attribute on an element

Next Post:

Access Control Request Headers, is added to header in AJAX request with jQuery

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error