Z
Z
zWaterFall2021-07-19 14:37:52
JavaScript
zWaterFall, 2021-07-19 14:37:52

How to write character validation in a string?

There is such an object, where the keys are the strings that need to be validated
. After applying the written method, valid / invalid should be displayed.
Open brackets should be matched with closed ones and quotes are parsed in pairs

const inputs = {
  'erh43t26s5jemaslvjhgjfd': true, //valid
  'erh43"#tje#(maslg)fvjh"gjfd': true, //valid
  'erh43"tje(maslg"fvjh)gjfd': false, //invalid
  'erh43"tje(maslg"fvj"h)gj"fd': true, //valid !!!!!
  'erh4{3tjem[aslvv]jhg}jfd': true, //valid
  'e"r"h"{4(3dsad)tj324ema}"slv"jhg"jfd': true, //valid
  'e"r[h"{4(3dsad)tj324ema}"slv]jhg"jfd': true, //valid !!!!!
  'e"r["h"{4(3dsad)tj324ema}slv]jhg"jfd': true, //valid !!!!!
  'erh4\'3tj(er#y{ut#maslv)jh}\'gjfd': false, //invalid
  'erh4\'3tj(ery{utmas}lv)jh\'gjfd': true, //valid
  'erh4\'3\'tj(ery{utmas}lv)jh\'gjfd': false, //invalid !!!!!
  'erh4\'3\'tj(ery{utmas}lv)jh\'gjfd': false, //invalid !!!!!
  'erh4\'3\'"t"j(ery{utmas}lv)\'jh\'gjfd': true, //valid 
  'erh4\'3\'"tj(ery{utmas}lv)j"h\'gj\'fd': true, //valid
  'erh43#tjecmasy565lv#jhgjfd': true, //valid
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2021-07-19
@zWaterFall

First, make a list of paired characters - brackets and quotes.
Next, character by character, iterate over the string
. If there is an opening character, push it onto the stack.
If you meet a closing one, check if its pair is at the top of the stack. If there is - remove the opening one from the stack, if not - an error.
At the end of the line, check if the stack is empty. If not empty, there are unclosed brackets or quotes left.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question