javascript replace string

      No Comments on javascript replace string

javascript replace string

GTU WTAD Practical 8 : Write a JavaScript to find a string from the given text. If the match is found then replace it with another string.

javascript replace string

This program demonstrate use of string related function to find out sub string from the main string. This function is used to search any kind of keyword from the main string.  This function return the position of the searched keyword and you can able to take some actions based on the same that string is found or not.

Jump to GTU WTAD Practical 9

File Name : index.jsp

File Location : Tomcat_Apache\webapps\gtu08\

<html>
<head>
	<title> javascript replace string : GTU WTAD Practical 8 : Write a JavaScript to find a string from the given text. If the match is found then replace it with another string.  </title>

	<script language="javascript">
		
			var str1,str2;
			function compare()
			{
				str1=f1.txt1.value;
				str2=f1.txt2.value;
				
				if(str1==""||str2=="")
				{
					if(str1=="")
					{
						document.getElementById("msg1").innerHTML="Please enter first string";
						return;
					}
					else if(str2=="")
					{
						document.getElementById("msg2").innerHTML="Please enter second string";
						return;
					}
				}
				if(str1.indexOf(str2)<0)
				{
					document.getElementById("msg3").innerHTML="String not found";
				}
				else
				{
					document.getElementById("msg4").innerHTML="String found";
					f1.b2.disabled=false
					f1.txt3.disabled=false
					f1.txt3.Focus();
					f1.txt3.value=" ";	
				}
			}
			function replace_str()
			{
				str3=f1.txt3.value
				str=str1.replace(f1.txt2.value, f1.txt3.value);
				document.getElementById("msg").innerHTML="Output String :: <h3>"+str;
				f1.b2.disabled=true
				f1.txt1.onFocus();
			}

	</script>
</head>
<body>
	<center> <h2> GTU WTAD Practical 8 : Write a JavaScript to find a string from the given text. If the match is found then replace it with another string.   </h2></center>
	
	<table>
	
	
		<form name="f1">
		
		<tr>
			<td> Enter String :   </td>
			<td> <input type="text" name=txt1 size=20> 
				<font color="blue">
				<div id="msg1">
				</div>
				</font>
			</td>
		</tr>
		
		<tr>
			<td>	Enter Sub String: </td>
			<td>
				<input type="text" name=txt2 size=20>
				<font color="red">
				<div id="msg2">
				</div>
				</font>
			</td>
		</tr>
	
		<tr>
			<td> Enter string which you want to Replace: </td>
			<td>
				<input type="text" name=txt3 size=20 disabled="true">
				<font color="yellow">
				<div id="msg3">
				</div>
				</font><br>
				<font color="red">
				<div id="msg4">
				</div>
				</font>
			</td>
		</tr>
		</table>
		
			<input type=button value="Find" name=b1 onClick="compare()">
			<input type=button value="Replace" name=b2 onClick="replace_str()" disabled="true">
			<input type=reset value="Clear" name=b3>
			<br>	<br>
			<font color="blue">
				<div id="msg">
				</div>
			</font>
			</b>
		</form>
</body>
</html>

 Output

GTU WTAD Practical 8

 

 

 

 

Leave a Reply