Skip to main content

JS Regex Helper

· 9 min read
Anand Techie
Software Developer, Frontend

A regular expression , also known as regex or regexp , is a sequence of characters that define a search pattern. It can be used to search, edit, or manipulate text and data.

In JavaScript, regular expressions are created using the RegExp object . The RegExp object has a number of methods that can be used to search for and manipulate strings.

Here are some examples of regular expressions in JavaScript:

// Find all occurrences of the letter "a" in a string
const regex = /a/g;
const string = "This is a string.";
const matches = regex.exec(string);

// Replace all occurrences of the letter "a" with the letter "e" in a string
const regex = /a/g;
const string = "This is a string.";
const newString = string.replace(regex, "e");

// Check if a string contains a particular pattern
const regex = /^abc/g;
const string = "abc123";
const match = regex.test(string);

Which came first?

· One min read
Anand Raja
Senior Software Engineer
Anand Techie
Software Developer, Frontend

You've heard the age-old riddle: "Which came first: the chicken or the egg?" Taken metaphorically, it's a meditation on the futility of determining the cause of a self-perpetuating cycle. Taken literally, it's a great question for evolutionary biologists.

Chickens come from eggs, but eggs come from chickens. So which came first?

Centering a Text Inside a Button

· One min read
Anand Raja
Software Developer
Anand Techie
Software Developer, JS Specialist

Instead of centering headings and paragraphs, let’s center text inside another element (<button> </button>). Aligning the button, and the text inside the button, would differ slightly from the examples.

That’s because the text-align property only works on the content inside block-level elements, such as headings and paragraphs, and not inline elements, such as buttons.

So here's what we'll do instead:

  • Wrap the button in a div
  • Then, add an inline style declaration to the div, i.e. <div style="text-align:center">.