WHAT'S NEW?
Loading...

Using string - localecompare(), match(), replace(), search(), slice() in javascript with example - Tech-n-Savvy Blogger


localeCompare ()

This method returns a number indicating whether a reference string comes before or after or is the same as the given string in sorted order.

Syntax

The syntax of localeCompare() method is:

string.localeCompare( param )

Argument Details

param : A string to be compared with string object.

Return Value

·         0 : If the string matches 100%.

·         1 : no match, and the parameter value comes before the string object's value in the locale sort order

·         -1 : no match, and the parameter value comes after the string object's value in the local sort order



Example

Try the following example.

<html>

<head>

<title>JavaScript String localeCompare() Method</title>

</head>

<body>

<script type="text/javascript">

var str1 = new String( "This is beautiful string" );
var index = str1.localeCompare( "XYZ" ); document.write("localeCompare first :" + index );


document.write("<br />" );


var index = str1.localeCompare( "AbCD ?" ); document.write("localeCompare second :" + index );


</script>

</body>

</html>

Output

localeCompare first :-1

localeCompare second :1


match ()

This method is used to retrieve the matches when matching a string against a regular expression.



Syntax

Use the following syntax to use the match() method.

string.match ( param )

Argument Details

param : A regular expression object.

Return Value

·         If the regular expression does not include the g flag, it returns the same result as regexp.exec(string).

·         If the regular expression includes the g flag, the method returns an Array containing all the matches.

Example

Try the following example.


<html>

<head>

<title>JavaScript String match() Method</title>

</head>

<body>

<script type="text/javascript">

var str = "For more information, see Chapter 3.4.5.1"; var re = /(chapter \d+(\.\d)*)/i; var found = str.match( re );


document.write(found );


</script>

</body>

</html>

Output

Chapter 3.4.5.1,Chapter 3.4.5.1,.1


replace ()

This method finds a match between a regular expression and a string, and replaces the matched substring with a new substring.

The replacement string can include the following special replacement patterns:

Pattern
Inserts







$$
Inserts a "$".




$&
Inserts the matched substring.




$`
Inserts  the  portion  of  the  string  that  precedes  the  matched


substring.




$'
Inserts  the  portion  of  the  string  that  follows  the  matched







substring.

$n or $nn        Where n or nn are decimal digits, inserts the nth parenthesized submatch string, provided the first argument was a RegExp object.


Syntax

The syntax to use the replace() method is as follows:

string.replace(regexp/substr, newSubStr/function[, flags]);

Argument Details

·         regexp : A RegExp object. The match is replaced by the return value of parameter #2.

·         substr : A String that is to be replaced by newSubStr.

·         newSubStr : The String that replaces the substring received from parameter #1.

·         function : A function to be invoked to create the new substring.

·         flags : A String containing any combination of the RegExp flags: g - global match, i - ignore case, m - match over multiple lines. This parameter is only used if the first parameter is a string.



Return Value

It simply returns a new changed string.

Example

Try the following example.

<html>

<head>

<title>JavaScript String replace() Method</title>

</head>

<body>

<script type="text/javascript">


var re = /apples/gi;


var str = "Apples are round, and apples are juicy."; var newstr = str.replace(re, "oranges");


document.write(newstr );


</script>

</body>

</html>

Output

oranges are round, and oranges are juicy.

Example

Try the following example; it shows how to switch words in a string.

<html>

<head>

<title>JavaScript String replace() Method</title>

</head>

<body>

<script type="text/javascript">


var re = /(\w+)\s(\w+)/;

var str = "zara ali";

var newstr = str.replace(re, "$2, $1"); document.write(newstr);


</script>

</body>

</html>

Output

ali, zara


Search ()

This method executes the search for a match between a regular expression and this String object.

Syntax

Its syntax is as follows:

string.search(regexp);

Argument Details

regexp : A regular expression object. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).

Return Value

If successful, the search returns the index of the regular expression inside the string. Otherwise, it returns -1.

Example

Try the following example.

<html>

<head>

<title>JavaScript String search() Method</title>

</head>

<body>

<script type="text/javascript">


var re = /apples/gi;

var str = "Apples are round, and apples are juicy.";


if ( str.search(re) == -1 ){ document.write("Does not contain Apples" );

}else{

document.write("Contains Apples" );

}


</script>

</body>

</html>

Output

Contains Apples


slice ()

This method extracts a section of a string and returns a new string.

Syntax

The syntax for slice() method is:

string.slice( beginslice [, endSlice] );

Argument Details

·         beginSlice : The zero-based index at which to begin extraction.

·         endSlice : The zero-based index at which to end extraction. If omitted, slice extracts to the end of the string.

Return Value

If successful, slice returns the index of the regular expression inside the string.

Otherwise, it returns -1.



Example

Try the following example.

<html>

<head>

<title>JavaScript String slice() Method</title>

</head>

<body>

<script type="text/javascript">


var str = "Apples are round, and apples are juicy.";


var sliced = str.slice(3, -2);


document.write( sliced );


</script>

</body>

</html>

Output

les are round, and apples are juic


0 comments:

Post a Comment