InsideDarkWeb.com

I am trying to create a regex for a list of accepted symbols and character string

I am building a test function that should only accept the following:

-
*
+
.
,
$
9
Z
V
B
CR – only once not with DB
DB – only once not with CR

I have gotten the first few to work like so: new RegExp(/^([-*+.,$9ZVB]|CR|DB)*$/gm), but I annot get the regex to only allow the CR or DB to appear only once either at beginning or end of the string. Any suggestions?

Test at https://regex101.com/r/1qT86R/2

Stack Overflow Asked on November 15, 2021

2 Answers

2 Answers

I'm guessing you are using javascript.

You can use the regex:

^(?:(?:CR|DB)[-*+.,$9ZVB]+|[-*+.,$9ZVB]+(?:CR|DB))$

Demo & explanation

var test = [
    '$---,---,--9.99BCR',
    '$---,---,--9.99BDB',
    'CR$---,---,--9.99B',
    'DB$---,---,--9.99B',
    'CR$---,---,--9.99BCR',
    'CR$---,---,--9.99BDB',
];
console.log(test.map(function (a) {
  return a+' : '+/^(?:(?:CR|DB)[-*+.,$9ZVB]+|[-*+.,$9ZVB]+(?:CR|DB))$/.test(a);
}));

Answered by Toto on November 15, 2021

Try this expression:

^(CR|DB)[-*+.,$9ZVB]+$|^[-*+.,$9ZVB]+(CR|DB)$

or

^((CR|DB)[-*+.,$9ZVB]+|[-*+.,$9ZVB]+(CR|DB))$

You can test it here: https://regexr.com/58tf1

Answered by Ilya Lysenko on November 15, 2021

Add your own answers!

Related Questions

javascript get Pacific Timezone

1  Asked on December 7, 2020

   

Why define a struct with single private field of unit type?

1  Asked on December 6, 2020 by m-bat

 

Read more Laravel

1  Asked on December 6, 2020 by user11916445

   

npx: the shell-auto-fallback argument has been removed

2  Asked on December 6, 2020 by mike-earley

         

Cross-Process Memory Filesystem In Python

0  Asked on December 6, 2020 by eliran-abdoo

         

HTML Template not rendering on screen

1  Asked on December 6, 2020 by ojm

     

WOE in a magrittr Pipe

2  Asked on December 6, 2020 by gojomoso

 

Convert object of arrays to array of objects

1  Asked on December 5, 2020 by drmrbrewer

       

Remove item from state

3  Asked on December 5, 2020 by clayton-crockville

 

How to concatenate 3 lists even if there are a list null?

2  Asked on December 5, 2020 by alexz

       

Angular 8: Select first element of ng-content

1  Asked on December 5, 2020 by ijustdev

     

docker-compose volume empty

3  Asked on December 5, 2020 by kerby82

   

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.