Click here to Skip to main content
15,912,285 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
<script type="text/javascript">
var str = "abc";
var patt1 = /^[a-z]$/;
if(patt1.test(str))
{
document.write(str);
alert(str);
}
</script>


i want to make regular expression for string if it match then alert
what problem in this code?
Posted
Updated 19-Jul-12 23:43pm
v4
Comments
StianSandberg 20-Jul-12 3:31am    
what is your expected output?
TorstenH. 20-Jul-12 5:44am    
I changed the tag so it will be found by JS developers and not by Java or script people who are not able to tell anything about.

Problem is with your regex expression used.

Try:
JavaScript
var str = "abc";
var patt1 = /[a-z]/i;

//Tests if the given string matches the Regexp, and returns true if matching, false if not.
if(patt1.test(str))
{
  document.write(str);
  alert(str);
}


See details about JavaScript Regex object here: http://www.w3schools.com/jsref/jsref_obj_regexp.asp[^]
 
Share this answer
 
Comments
Espen Harlinn 21-Jul-12 7:52am    
Nice reply :-D
Hi this is working fine, please check and post error message.
 
Share this answer
 
Comments
nilesh026 20-Jul-12 3:33am    
sorry its my mistake now trace
Try this
XML
<script type="text/javascript">
var str = "abc";
var patt1 = /^[a-z]$/;
if(str.search(pat1) != -1) {
  document.write(str);
  alert(str);
}
</script>

Happy Coding :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900