JavaScript RegExp return values between various square brackets? How to get value brackets?

Disha Verma
Updated on 15-Mar-2026 23:18:59

2K+ Views

Extracting values between various square brackets can be done using JavaScript regular expressions. The regular expressions in JavaScript are useful for extracting values enclosed within different types of brackets, such as square brackets ([ ]), curly brackets ({ }), and parentheses "( )". In this article, we'll look at how to use JavaScript's RegExp to find and get content that is inside square brackets easily. Understanding JavaScript RegExp Regular expressions (RegExp) are patterns used to match and manipulate strings in JavaScript. JavaScript RegExp helps you find and get text that is inside specific brackets by using patterns. ... Read More

Summing numbers from a string - JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:18:59

1K+ Views

We are required to write a JavaScript function that takes in a string that contains some one-digit numbers in between and the function should return the sum of all the numbers present in the string. Let's say the following is our string with numbers: const str = 'gdf5jhhj3hbj4hbj3jbb4bbjj3jb5bjjb5bj3'; Method 1: Using for Loop with Type Conversion This approach splits the string into characters and checks each one using type conversion: const str = 'gdf5jhhj3hbj4hbj3jbb4bbjj3jb5bjjb5bj3'; const sumStringNum = str => { const strArr = str.split(""); let ... Read More

JavaScript - Writing a string function to replace the kth appearance of a character

AmitDiwan
Updated on 15-Mar-2026 23:18:59

309 Views

Let's say, we are required to write a String.prototype function that takes in three arguments. First argument is string that should be searched for substrings Second argument is the string, the occurrence of which String to be removed Third argument is a Number say n, nth occurrence of substring to be removed from string. The function should return the new string if the removal of the subStr from the string was successful, otherwise it should return -1 in all cases. Example Following is the code − const str = 'jkdsttjkdsre'; const subStr ... Read More

How to create a session only cookies with JavaScript?

Vrundesha Joshi
Updated on 15-Mar-2026 23:18:59

1K+ Views

Session cookies are temporary cookies that expire when the browser session ends (when the user closes the browser). Unlike persistent cookies, they don't have an expiration date set and are automatically deleted when the browser is closed. Creating Session Cookies To create a session cookie in JavaScript, simply omit the expires or max-age attribute when setting the cookie: // Session cookie - no expiration date document.cookie = "sessionUser=johnDoe; path=/"; // Another session cookie with additional attributes document.cookie = "tempData=someValue; path=/; secure; samesite=strict"; console.log("Session cookies created"); console.log("Current cookies:", document.cookie); Session ... Read More

Set the font size with CSS

Sravani S
Updated on 15-Mar-2026 23:18:59

360 Views

The font-size property in CSS controls the size of text in your web pages. You can specify font sizes using various units and keyword values to create visual hierarchy and improve readability. Syntax font-size: value; Font Size Values The font-size property accepts several types of values: Keywords: xx-small, x-small, small, medium, large, x-large, xx-large Relative keywords: smaller, larger Units: pixels (px), percentages (%), em, rem Example: Different Font Size Values Font Size Examples This font ... Read More

How to separate alphabets and numbers from an array using JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:18:59

1K+ Views

We have an array that contains some number literals and some string literals mixed, and we are required to write a sorting function that separates the two and the two types should also be sorted within. This approach uses a custom comparator function with JavaScript's sort() method to first separate numbers from strings, then sort each type individually. Using Custom Sort Comparator The code for this sorting function will be: const arr = [1, 5, 'fd', 6, 'as', 'a', 'cx', 43, 's', 51, 7]; const sorter = (a, b) => { ... Read More

Nearest palindrome in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:18:59

814 Views

We are required to write a function that takes in a number n and returns a palindromic number that is nearest to the number n. A palindrome reads the same forwards and backwards. For example: If the input number is 264, then the output should be 262 If the input number is 7834, then the output should be 7887 Approach The approach divides the number into two halves based on its length and creates a palindrome by mirroring the first half. For odd-length numbers, the middle digit remains unchanged. Example const ... Read More

How do I display the content of a JavaScript object in a string format?

AmitDiwan
Updated on 15-Mar-2026 23:18:59

341 Views

To display JavaScript object content as a string, you have several methods. The most common approach is using JSON.stringify() which converts objects into readable JSON format. Using JSON.stringify() The JSON.stringify() method converts JavaScript objects into JSON strings: Display JavaScript Object // Create a JavaScript object var ... Read More

Switch case calculator in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:18:59

9K+ Views

Let's say, we are required to write a JavaScript function that takes in a string like these to create a calculator − "4 add 6" "6 divide 7" "23 modulo 8" Basically, the idea is that the string will contain two numbers on either sides and a string representing the operation in the middle. The string in the middle can take one of these five values − "add", "divide", "multiply", "modulo", "subtract" Our job is to return the correct result based on the string Syntax switch (expression) { ... Read More

Replacing all special characters with their ASCII value in a string - JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:18:59

2K+ Views

We are required to write a JavaScript function that takes in a string that might contain some special characters. The function should return a new string with all special characters replaced with their corresponding ASCII value. Understanding Special Characters Special characters are symbols that are not letters, numbers, or spaces. Examples include !, @, #, $, etc. Each character has a unique ASCII code that can be retrieved using the charCodeAt() method. Example Following is the code: const str = 'Th!s !s @ str!ng th@t cont@!ns some special characters!!'; const specialToASCII = str ... Read More

Advertisements