I have a Laravel 9 forum project and I have added this form field:
<div class="create__section">
<label class="create__label BMehrBold" for="tags">Tags (separate each one by [space])</label>
<input type="text" class="form-control BKoodakBold" id="tags" placeholder="Enter the keywords here">
<span id="tagshow"></span>
</div>
So as you can see I have said that Each tag should be separated by space.
So if user enters javascript
for example as value, it should show this at the tagshow
section:
#javascript
Then he should be able to enter other keywords as well and separate each one by [space]:
#javascript
#jquery
…
But I need to define when the user press [space] on keyboard in order to get the tag value of the input.
So how can I do that?
4
Answers
You can do it with
split()
:Yep,
.split()
and.map()
are your friends here:You can use String.prototype.replace() and use the Pattern
$1
to embed your match with the added prefix#
. The regex/([^ ]+)/g
or/(S+)/g
would match anything that is not a space (your tag words)the nice thing about the above solution is that you can also wrap your tags into
SPAN
elements as tags pills pretty easily:You can try this: