Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Object Oriented Programming Articles
Page 82 of 589
How to remove html tags from a string in JavaScript?
Removing HTML tags from strings is a common task in JavaScript web development. You can accomplish this using regular expressions to match and replace HTML tag patterns with empty strings. Understanding HTML Tag Structure HTML elements are enclosed between angle brackets like , , , etc. By targeting the pattern of content within these brackets, we can effectively strip all HTML tags from a string. Syntax str.replace(/(]+)>)/ig, ''); The regular expression /(]+)>)/ig breaks down as: ]+) - matches one or more characters that aren't closing brackets > - matches closing angle bracket ...
Read MoreHow to count a number of words in given string in JavaScript?
Counting words in a JavaScript string requires handling various whitespace scenarios like multiple spaces, leading/trailing spaces, and newlines. Using regular expressions provides an effective solution. The Challenge Strings can contain irregular spacing that affects word counting: Leading and trailing spaces Multiple consecutive spaces between words Newlines with spaces Empty strings Step-by-Step Approach Step 1: Remove Leading and Trailing Spaces Use regex to eliminate spaces at the start and end of the string: str.replace(/(^\s*)|(\s*$)/gi, ""); Step 2: Reduce Multiple Spaces to Single Space Replace consecutive spaces with a ...
Read MoreHow to access a function property as a method in JavaScript?
In JavaScript, object properties can hold functions, which become methods when accessed through the object. A method is simply a function that belongs to an object and can access the object's other properties using the this keyword. Syntax const object = { property1: value1, property2: value2, methodName: function() { return this.property1 + this.property2; } }; // Call the method object.methodName(); Example 1: Employee Object with fullName Method Here's an ...
Read MoreHow to hide e-mail address from an unauthorized user in JavaScript?
Hiding email addresses from unauthorized users helps protect privacy and reduce spam. JavaScript provides several methods to obfuscate email addresses while keeping them readable for legitimate users. Method 1: Partial Masking with Asterisks This approach replaces part of the username with asterisks, keeping the domain visible: function hideEmail(email) { var parts = email.split("@"); var username = parts[0]; var domain = parts[1]; if (username.length 2 ? ...
Read MoreHow to remove non-word characters in JavaScript?
To remove non-word characters in JavaScript, we use regular expressions to replace unwanted characters with empty strings. Non-word characters include symbols, punctuation, and special characters that aren't letters, numbers, or underscores. What are Non-Word Characters? Non-word characters are anything that doesn't match the \w pattern in regex. This includes symbols like !@#$%^&*(), punctuation, and special characters, but excludes letters (a-z, A-Z), digits (0-9), and underscores (_). Using Simple Regex Pattern The most straightforward approach uses the \W pattern, which matches any non-word character: function removeNonWordChars(str) { if ...
Read MoreHow to decode an encoded string in JavaScript?
In JavaScript, string decoding refers to converting encoded strings back to their original form. While escape() and unescape() were historically used, modern JavaScript provides better alternatives like decodeURIComponent() and decodeURI(). Using unescape() (Deprecated) The unescape() method decodes strings encoded by the escape() method. It replaces hexadecimal escape sequences with their corresponding characters. Syntax unescape(string) Example // Special character encoded with escape function var str = escape("Tutorialspoint!!"); document.write("Encoded : " + str); document.write(""); ...
Read MoreHow to convert a string in to a function in JavaScript?
To convert a string into a function in JavaScript, the eval() method can be used. This method takes a string as a parameter and evaluates it as JavaScript code, which can include function definitions. Syntax eval(string); Example: Converting String to Function In the following example, a string contains a JSON object where the 'age' property holds a function as a string. Using eval(), we convert this string into an actual executable function. var string = '{"name":"Ram", "age":"function() {return 27;}", "city":"New jersey"}'; var fun ...
Read MoreHow to parse an URL in JavaScript?
JavaScript provides several methods to parse URLs and extract their components like protocol, hostname, pathname, and query parameters. The modern approach uses the built-in URL constructor, while legacy methods involve creating DOM anchor elements. Using the URL Constructor (Modern Approach) The URL constructor is the standard way to parse URLs in modern JavaScript. It creates a URL object with all components easily accessible. const url = new URL("https://www.youtube.com/watch?v=tNJJSrfKYwQ"); console.log("Protocol:", url.protocol); console.log("Host:", url.host); ...
Read MoreHow to add two strings with a space in first string in JavaScript?
To concatenate two strings in JavaScript, we use the '+' operator. When the first string already contains a trailing space, we can directly concatenate without adding an explicit space. However, if there's no space, we need to add one manually between the strings. Method 1: First String Has Trailing Space When the first string already ends with a space, simple concatenation is sufficient: function concatenateStrings(str1, str2) { return (str1 ...
Read MoreWhat is the use of Object.is() method in JavaScript?
The Object.is() method determines whether two values are strictly equal, providing more precise comparison than the regular equality operators. Syntax Object.is(value1, value2) Parameters value1: The first value to compare value2: The second value to compare Return Value Returns true if both values are the same, false otherwise. How Object.is() Determines Equality Two values are considered the same when they meet these criteria: Both are undefined or both are null Both are true or both ...
Read More