DEV Community


How to find a count of specific character in a string and take out dynamic value out of a repeating {{}} character in JavaScript

Muhammad Uzair
ReactJs | React-Native | JavaScript | Nodejs | MongoDB | UserInterface | Python
・2 min read

I was working in JavaScript since last year,I have taken in a great experiences from various situations,As a developer we are facing new scenarios everyday and learning new things with every rising sun, Like yesterday i was supposed to solve a problem to find a count of specific character {{}} repeating in my string and to catch a value in it which will always be different depends on user input.

Problem 1: Finding the count {{}} this in my string

Alt Text

It will first finds that either two curly brackets opening at the same time then it will return an array contains all the possible matches .The g in it is the parameter of Regular Expression it will search globally.

Problem 2 : Taking the dynamic values out of {{}} from a string based on user input

Alt Text

In this case i have applied this function on my input box if its value changes, the function checks that if curly brackets occur then it will return an array.
If there will be one match found then it will return the value in array with an index[0] but if there may be more than one match found it was returning an array with first match at index 0 and then two empty strings on index 1 and index 2 and then second match on index 3,it repeats this behavior after every match , So that's why i applied a loop with i+=3 at every iteration it will take a step of 2.

May be it was an expensive method but it solved my problem,As I have not worked much with Regular Expressions so that's why may be it was scrambled and time taken process but it worked for me.

Hope you find this article useful. Please share your thoughts or if there is any method by which this can be done easily in the comment section.


Discussion (0)

Forem Open with the Forem app