V
V
Viktor Putin2021-07-20 06:00:10
JavaScript
Viktor Putin, 2021-07-20 06:00:10

How to validate such a hash tag?

There is such a function for validating a field with hash tags - https://codepen.io/viktoraz/pen/KKmqLKK?editors=1010

Hash tags must be separated by a space and each must match the pattern.
1 problem is that the regular expression does not check every hash tag, that is, it allows you to enter the first hash tag without a grid, if the last one is with a grid.
2 problem is to clear the field. If you enter any characters, and then clear the field, the field will still not be valid, although it is empty (checked)

I assume that the problem is just in the regular expression, but I tried many different ones, nothing fits. Is there another way to validate hash tags?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Bereznikov, 2021-07-20
@Ambi2Rush

Alternatively, you could make a slightly more powerful regular expression that allows 1 or more valid hashtags separated by spaces. Very rude - add \s* to the end of your regular expression (before the end of the line) and wrap the whole thing in ()+.
For example, regex: (#(?<tag>[A-Za-zА-Яа-я]{1,19})\s*)+( https://regex101.com/r/rfH4AA/1 )
Then we get all the values ​​of the tag group, I'm not strong in js, but I think there must be a way.

A
Alexey Dubrovin, 2021-07-20
@alekcena

I don't understand what the problem is

if("ПОЛЕ ПУСТОЕ"){
//Не проверяете регуляркой
//Отправляете на сервак
}

Next you said:

Hash tags must be space separated

Get all hashtags from user string
let input = "#adsasddasd #adsasdasda #gadsqw-asdas"

let arrHashTag = input.split(" ");

I think further to check each individual element for
pattern matching

It won't be difficult

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question