-3

I am new to regex. I need a regex for JavaScript code that allows special character * in the beginning of text input, but not anywhere else in the text.

example : it should allow *text, it should not allow *text*abcd

I would need a similar regex for PHP as well.

I have tried using "/^\s*/", but this doesn't work. Anyways I dont have anyidea of regex. I have started learning it.

Thanks Dora

Dora
  • 292
  • 3
  • 15
  • 1
    -1 http://www.emilvikstrom.se/whyidownvote.html – Emil Vikström Jun 05 '12 at 12:58
  • 4
    * See also [Open source RegexBuddy alternatives](http://stackoverflow.com/questions/89718/is-there) and [Online regex testing](http://stackoverflow.com/questions/32282/regex-testing) for some helpful tools, or [RegExp.info](http://regular-expressions.info/) for a nicer tutorial. – mario Jun 05 '12 at 12:59
  • 1
    Obviously this question needs some more (nearly identical) answers, 7 isn't enough. ;-) – Qtax Jun 05 '12 at 13:25

7 Answers7

5

Part of learning how to use regex is knowing when you don't need it. In this case a solution without regex would suffice, for PHP:

if (strrpos($str, '*') > 0) {
  // invalid position of *
}

For JavaScript:

if (str.lastIndexOf('*') > 0) {
    // invalid position for *
}

It basically finds the last position of the special character; if it appears in the string and is not the first character, the code inside the condition gets executed.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • Tempted to give an extra downvote for downvote whining. Anyway, this wasn't asked for. It's a tag rot contribution, and not useful for the Javascript case requested above. And there's the impression this answer is just due to anti-regex or microoptimization memes. – mario Jun 05 '12 at 13:05
  • While I see @mario's point, I think it's a good alternative. Why regex when you don't have to? However, I think you mean "*A solution **without** regex...*" – Dan Lugg Jun 05 '12 at 13:07
  • @mario ah right, forgot the JavaScript case; this is not anti-regex, but just how I normally deal with string problems. – Ja͢ck Jun 05 '12 at 13:10
  • @Bracketworks right, just reworded the first sentence ;-) thanks! – Ja͢ck Jun 05 '12 at 13:11
  • Hey thanks a lot. I was in great hurry and your answer sorted my problem – Dora Jun 05 '12 at 13:31
  • @mario After adding the JS variant, which I forgot, I fail to see how this is not what was asked for: a condition that determines whether a character - if present - only appears at the start of a string. And i don't understand the tag rot classification either. – Ja͢ck Jun 05 '12 at 13:35
  • Yes, you solved OPs immediate problem. It satisfies the plzsendtehcodez. Yet the question will show up in Google as [regex] question, but with a [strpos] answer (well, if it wasn't for the other on-topic answers, of course). I fail to see why this isn't tag rot. You don't know if the next asker tripping over this page won't in fact need to assert a bit more than just the first character. – mario Jun 05 '12 at 13:44
3
^\*?[^*]+$
  • The ^ means match from the start of the string
  • The \*? optionally matches a * (the ? means zero-or-one matches)
  • The [^*]+ matches one-or-more characters that are not *
  • The $ means that the match has to end at the very end of the string, to make sure there aren't any extra *s

This can be used in PHP and Javascript.

huon
  • 94,605
  • 21
  • 231
  • 225
2

Should get you there.

^\*[^\*]+$    // forces the first asterisk

^\*?[^\*]+$   // allows the first asterisk
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
1

if I am understanding well, I think that you should just scape the * like this \*, also remember to write this \\* in case that you are writting the regex in a string

MARP
  • 581
  • 1
  • 12
  • 19
0

Try this code:

/^(\*)?[^*]+/

The ^ symbol means in the beginning

Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
  • This matches `*text*abcd` (which is explicitly disallowed). – huon Jun 05 '12 at 12:59
  • This expression *requires* an `*` at the beginning, which I don't think is what is wanted. It says "allows for" not "requires". I suppose it can be interpreted either way. – vcsjones Jun 05 '12 at 13:02
  • Well, this is what I understood. The question is misconceived – Danilo Valente Jun 05 '12 at 13:08
  • yes the input having * in the beginning shall be allowed. But it is not compulsary to have * in the begining. – Dora Jun 05 '12 at 13:22
0

Give this expression a shot:

^.?[^*]+$
vcsjones
  • 138,677
  • 31
  • 291
  • 286
0

Try this regex:

/^\*[^\*]+$/g

Here is an example in jsFiddle.

kevlar1818
  • 3,055
  • 6
  • 29
  • 43