4 Replies - 560 Views - Last Post: 28 August 2012 - 05:25 AM

#1 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

regex expression

Posted 28 August 2012 - 04:12 AM

Hi guys. I am trying to do some real time validation, but because there are so many records, I am going to try and do a regex expression. Basically, I have an if statement which displays a tick if the condition is correct. What I need to do is have the expression check for the input in the following format
LOL - Laugh Out Loud
DWM - Dont Worry

So basically, the format should be 3 capital letters, followed by a space, hash, space, and then some other words. So I grab the input
var phase = $("input[name='phase']").val();


Then I need to try and get the regex expression
if(isValidExpression){
}


function isValidExpression(phase) {
      var pattern = new RegExp(??);
      return pattern.test(phase);
}


What would the regex for this format be?

Cheers

Nick

Is This A Good Question/Topic? 0
  • +

Replies To: regex expression

#2 Mina-no-Hime  Icon User is offline

  • D.I.C Head

Reputation: 98
  • View blog
  • Posts: 176
  • Joined: 23-August 12

Re: regex expression

Posted 28 August 2012 - 04:18 AM

I tested this one over at Rubular (http://rubular.com - brilliant Regex testing site), and it seemed to do the job:

\A[A-Z]{3} - [A-Za-z]\z

Was This Post Helpful? 1
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: regex expression

Posted 28 August 2012 - 04:55 AM

Kool, didnt know about that site, will be helpful in the future. I have added it so its like
var pattern = new RegExp(\A[A-Z]{3} - [A-Za-z]\z);


But thats giving me a syntax error. I dont know if I need extra () somewhere?

Cheers
Was This Post Helpful? 0
  • +
  • -

#4 Mina-no-Hime  Icon User is offline

  • D.I.C Head

Reputation: 98
  • View blog
  • Posts: 176
  • Joined: 23-August 12

Re: regex expression

Posted 28 August 2012 - 05:08 AM

I'm not sure exactly how the RegExp object is expecting to receive it, but IIRC you can simplify the process by just using forward-slashes and removing the new RegExp call altogether:

var pattern = /[A-Z]{3} - [A-Za-z]+/;


Also, the RegEx is modified a little from before. It wasn't working as a direct translation to Javascript (not sure if that was an error on my part, or a difference between Ruby and Javascript). It should work for you like this.

I recommend taking a poke around this tutorial for more information.
Was This Post Helpful? 1
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: regex expression

Posted 28 August 2012 - 05:25 AM

kool, works perfectly, thanks for the info and link
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1