Properties of String: -
A String Object has these properties:
• Constructor – A constructor returns a reference to the String function.
• Length – Length property returns the length of the String.
- Prototype – A prototype property allows us to add properties and methods to an object.
Finding a String into a String – Two methods comes under this:
IndexOf() – This method returns the index of the first occurrence of a particular text in a String:
This method returns the position of the first occurrence of a specified text and that is : 7
lastIndexOf() – It is as same as IndexOf, only it starts from the right and moves left:
This method returns the position of the last occurrence of the specified text and that is: 21
search(): - The Search method searches the string for a specified value and returns its position
This method returns the position of the first occurrence of a specified text in the string and that is: 7
Extracting String Methods:
We use three methods for extracting a part of string:
1. Slice Method
2. substring Method
3. substr Method
slice()– This method extracts a part of String and it returns a extracted part in a new string. This method uses two parameters the starting position and the ending position.
This method extracts a part of the string and returns the extracted part of the string as a new string and that is “Banana”
substring() - This method is as same as the Slice Method, the only difference is that it does not accept negative indexes.
This method extracts a part of the string and returns the extracted part in a new string and that is: “Banana”
replace(): - This method replaces the specified value with another value in a string:
In this output on clicking the button Microsoft will be replaced with Google.
These Methods does not end up here, rather there are even more methods to manipulate the string. We are just covering most important and useful one.
Regular Expression: -When we search any data in the text, we can use search pattern to describe what we are searching for. It is a sequence of characters that forms a search pattern. It can be a single character or more. Also, it can be used for performing all types of text search and text replace operations.
Many a times regular expressions are used with String Methods and are search() and replace().
Regular Expression Modifiers: - Modifiers can be used to perform case-intensive matching.
i = for performing case-intensive matching.
g = for performing a global matching.
m = for performing multiline matching.
Regular Expression Patterns: - Brackets: These are used to find a range of characters
[abc] Find any of the characters between the brackets
[0-9] Find any of the digits between the brackets
(x|y) Find any of the alternatives separated with |
Metacharacters: These characters are special meaning characters
\d Find a digit
\s Find a whitespace character
\b Find a match at the beginning or at the end of a word
\uxxxx Find the Unicode character specified by the hexadecimal number xxxx
Quantifiers: Defines the Quantities
n+ Matches any string that contains at least one n
n* Matches any string that contains zero or more occurrences of n
n? Matches any string that contains zero or one occurrences of n
var name = /pattern/modifiers;
Methods in Regular Expression: -test() Method:-
test() method is a regular expression method. This method searches a string for a pattern and returns True or False, it depends on the result.
In this output on clicking the button we get this output “true”.
This Method searches a string for a specified pattern, and returns the text found. If no match is found it returns the null.
In this output on clicking the button we will get the following answer ‘e’.
After going through the different methods of String Object and Regular Expression we can say these all these terms helps a lot in the programming a high-level UI and being a beginner, it is quite necessary to clear all the aspects carefully and then implement them in the script.