?
Solved

Followup on Regex

Posted on 2009-12-21
7
Medium Priority
?
304 Views
Last Modified: 2012-05-08
I need a regular expression and usage syntax in javascript to handle the following.  Return a "true" or something if a string first character is a [ AND the last character is a ].

I was given the following but it isnt working, this needs to be in javascript syntax.

RegExp("^\[.*\]$")
0
Comment
Question by:Paulconsulting
  • 3
  • 2
  • 2
7 Comments
 
LVL 27

Expert Comment

by:ddrudik
ID: 26096842

var str="[test]";
var patt=/^\[.*\]$/g;
var result=patt.test(str);
document.write("Returned value: " + result);

Open in new window

0
 

Author Comment

by:Paulconsulting
ID: 26096865
thanks ddrudik can you expand on that and put this in a regular expression variable like so? to return a true or false?

 var re = new RegExp("^\[.*\]$");
 
    if (val.match(re))
    {
      return true;
    }
    else
    {
      return false;
    }
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26096868
It is missing the a's

<script>
var reg = /^a.*a$/
alert(reg.test('asdfsdsdfa'))
alert(reg.test('asdfsdsdfb'))
alert(reg.test('bsdfsdsdfa'))

</script>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26096876
haha

Oops. Read that comPLETELY wrong ;)

Here is mine

<script>
var reg = /^\[.*\]$/
function checkBrackets(str) {
  return reg.test(str)
}
str = "[asdadasdasda]"
alert(str+':'+checkBrackets(str))
</script>
0
 
LVL 27

Accepted Solution

by:
ddrudik earned 2000 total points
ID: 26096949
Paulconsulting, if you want to return true/false upon test, here are two options you might consider:
1:

function testval(str){
	var re = new RegExp("^\[.*\]$");
	return re.test(str);
}

2:

function testval(str){
	return /^\[.*\]$/.test(str);
}

Open in new window

0
 

Author Closing Comment

by:Paulconsulting
ID: 31668560
Thanks!
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26097142
Erm... I think a split could have been in order here  ?

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question