i am using the below code

var value = 'abcd$';
var regularExpression= new RegExp('[-!$%^&*()_+|~=`{}\[\]:";\'<>?,.\/]');
if(regularExpression.test(value)) {

it's returning false, even the string contains a special character.

  • Is the laste character "e" line 2 is normal ?
    – Badbaxx
    Feb 8, 2021 at 9:45
  • My bad, it's a typo mistake. I have edited my question. Thanks.
    – Anna
    Feb 8, 2021 at 9:57

2 Answers 2


Try this


var output;

var value = 'abcd$';
var regularExpression= new RegExp('^.*[-!$%^&*()_+|~=`{}[\\]:";\'<>?,.\/]+.*$');
if(regularExpression.test(value)) {
   output = 'true';
   output = 'false';



The output


Here's another idea. I checked this in LWC component reference, and it works.


    <div class="slds-m-top_medium slds-m-bottom_x-large">
            <lightning-input type="text" onchange={check} label="Enter some text"></lightning-input>


import { LightningElement } from 'lwc';

export default class InputText extends LightningElement {
    has = false;

    check({detail}) {
        const value = detail.value
        this.has = /[-!$%^&*()_+|~=`{}\[\]:";\'<>?,.\/]+/.test(value);

The difference is how I define RegExp. Not sure why this works and yours don't, digging into this. WIll update the answer once find out.

const regexp = /[-!$%^&*()_+|~=`{}\[\]:";\'<>?,.\/]+/

instead of

const regexp = new RegExp('[-!$%^&*()_+|~=`{}\[\]:";\'<>?,.\/]');

Also, I tried your way in a NodeJS REPL and it doesn't work either. SO it's not an LWC thing

Update There's a problem with your RegExp. You should escape backslashes, which escape other characters. So you need to have the RegExp constructed the following way

new RegExp('[-!$%^&*()_+|~=`{}\\[\\]:";\\\'<>?,.\/]')

instead of

new RegExp('[-!$%^&*()_+|~=`{}\[\]:";\'<>?,.\/]')

I would recommend using a string expression instead of constructing a RegExp object


For more information look here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .