string.replace()

JavaScript String replace() method searches the occurrences of a given string pattern and replace it with a given substring. The String replace() method returns the new modified string and it will not affect the value of the original string itself. The search argument can be a Regular Expression or a string, and the replacement can be either a string or a JavaScript function.

Syntax

string.replace(searchValue,replaceValue)

example

	var str = "Test String";
	str.replace("String","Value");

The above code output as “Test Value”

Full Source

<html>
<body>
<script type="text/javascript">
	var str = "Javascript Tutorial";
	var newStr = str.replace("Tutorial","help");
	document.write(newStr);
</script>
</body>
</html>

string.charAt()

JavaScript String charAt() method returns the specified character from a string of the given index number. The index starts from 0 to string.length – 1. If the given index value is out of range, it will returns an empty string.

Syntax

string.charAt(index);

Source

	var str = "Javascript index";
	document.write(str.charAt(3));

The above code will return “a”.

string.charCodeAt()

JavaScript String charCodeAt() method returns the numeric Unicode value of specified character from a string of the given index number. If the given index value is out of range, it will returns an empty string.

Syntax

string.charCodeAt(index);

Full Source

	var str = "Javascript index";
	document.write(str.charCodeAt(3));

The above code will return 97, that is the numeric Unicode value of “a” is 97.

string.match()

JavaScript String match() searches a string for a match against a regular expression and returns an array. Each element of the array contains the string of each match that is found.

Syntax

string.match(regularExpression)

example

	var str = "twinkle twinkle little star";
	var retArray =str.match(/kle/g);
	document.write(retArray);

The above code will display “kle,kle”, because there are two occurrences of “kle” in the “twinkle twinkle little star”

string match() with wildcard

You can use javascript string match(). Periods are single wildcard characters

example

	var str = "Javascript";
	str.match(/Java....pt/))

Full Source

<html>
<body>
<script type="text/javascript">
	var str = "Javascript";
	if(str.match(/Java....pt/))
	{
		document.write("Match Found...!");
	}
	else
	{
		document.write("Not found any matches !");
	}
</script>
</body>
</html>