String object, methods and regular expression

Strings are simply groups of characters, like ‘Vskills’, ‘Hello world!’, or even ’23’. Strings in JavaScript, are always written with single quotes ‘ or double quotes ” around them and don’t mix up quotes. String literals (denoted by double or single quotes) and strings returned from String calls in a non-constructor context (i.e., without using the new keyword) are primitive strings. JavaScript automatically converts primitives and String objects, so that it’s possible to use String object methods for primitive strings. In contexts where a method is to be invoked on a primitive string or a property lookup occurs, JavaScript will automatically wrap the string primitive and call the method or perform the property lookup.

 

Creating a String object: It’s syntax is

Syntax Example                 
var val = new String(string); var a = new String(‘Hello world!’);

Adding special characters in a string needs to escape by placing a backslash \ before it.

String methods – Various string methods are listed below:

Methods Description
charAt(x) Returns the character at the “x” position within the string.
charCodeAt(x) Returns the Unicode value of the character at position “x” within the string.
concat(v1, v2,…) Combines one or more strings (arguments v1, v2 etc) into the existing one and returns the combined string. Original string is not modified.
fromCharCode(c1, c2,…) Returns a string created by using the specified sequence of Unicode values (arguments c1, c2 etc). Method of String object, not String instance. For example: String.fromCharCode().
indexOf(substr, [start]) Searches and (if found) returns the index number of the searched character or substring within the string. If not found, -1 is returned. “Start” is an optional argument specifying the position within string to begin the search. Default is 0.
lastIndexOf(substr, [start]) Searches and (if found) returns the index number of the searched character or substring within the string. Searches the string from end to beginning. If not found, -1 is returned. “Start” is an optional argument specifying the position within string to begin the search. Default is string.length-1.
match(regexp) Executes a search for a match within a string based on a regular expression. It returns an array of information or null if no match is found.
Replace( regexp, replacetext) Searches and replaces the regular expression portion (match) with the replaced text instead.
search(regexp) Tests for a match in a string. It returns index of match, or -1 if not found.
slice(start, [end]) Returns a substring of the string based on the “start” and “end” index arguments, NOT including the “end” index itself. “End” is optional, and if none is specified, the slice includes all characters from “start” to end of string.
split(delimiter, [limit]) Splits a string into many according to the specified delimiter, and returns an array containing each element. The optional “limit” is an integer that lets you specify the maximum number of elements to return.
substr(start, [length]) Returns the characters in a string beginning at “start” and through the specified number of characters, “length”. “Length” is optional, and if omitted, up to the end of the string is assumed.
substring(from, [to]) Returns the characters in a string between “from” and “to” indexes, NOT including “to” inself. “To” is optional, and if omitted, up to the end of the string is assumed.
toLowerCase() Returns the string with all of its characters converted to lowercase.
toUpperCase() Returns the string with all of its characters converted to uppercase.

Regular expressions – They are very powerful tools for performing pattern matches. This pattern language is mostly used for validation tasks and complex tasks. They are implemented in JavaScript by:

1) Using literal syntax as

var RegularExpression = /pattern/

2) When you need to dynamically construct the regular expression, via the RegExp() constructor as            var RegularExpression = new RegExp(“pattern”);

The RegExp() method can dynamically construct the search pattern as a string, and is useful when the pattern is not known ahead of time. Patterns are defined using string literal characters and metacharacters. For example, the following regular expression determines whether a string contains a valid 5-digit value or not

var re5digit=/^\d{5}$/

Characters used for making the regular expression are:

Brackets – Brackets are used to find a range of characters:

Expression Description
[abc] Find any character between the brackets
[^abc] Find any character not between the brackets
[0-9] Find any digit from 0 to 9
[A-Z] Find any character from uppercase A to uppercase Z
[a-z] Find any character from lowercase a to lowercase z
[A-z] Find any character from uppercase A to lowercase z
[adgk] Find any character in the given set
[^adgk] Find any character outside the given set
(red|blue|green) Find any of the alternatives specified

 

Metacharacters – Metacharacters are characters with a special meaning:

Metacharacter Description
. Find a single character, except newline or line terminator
\w Find a word character
\W Find a non-word character
\d Find a digit
\D Find a non-digit character
\s Find a whitespace character
\S Find a non-whitespace character
\b Find a match at the beginning/end of a word
\B Find a match not at the beginning/end of a word
\0 Find a NUL character
\n Find a new line character

 

Back to Tutorial

Share this post
[social_warfare]
Arrays object and methods
Introduction to Intellectual Property

Get industry recognized certification – Contact us

keyboard_arrow_up