1

Please can someone assist me with a regular expression to validate a string of this pattern

aaa [bbb]

I want to an input in the format expressed above. aaa & bbb can be any combination of one or more words which could also contain special characters. bbb must be contained inside square brackets ([...]). The full string can have a leading or trailing spaces.

I have tried this:

var re=/\w{0,} \[*\w{0,}\]/

But it returns false on a test string like:

re.test("onc>*!llklk[dd<dfd]")
1
  • Yes, but this /\w{0,} [*\w{0,}]/.test('aaa [bbb!]'); fails Commented Oct 9, 2014 at 13:31

3 Answers 3

2

Your regular expression explicitly requires a space to be present. We can visualise this with Regexper:

Example

This returns false on "onc>*!llklk[dd<dfd]" because there is no space character.

To fix your problem, either use a test string which has a space character, or change your regular expression to not require this character:

var re = /\w{0,}\[*\w{0,}\]/;
re.test("aaa [bbb]");
> true

re.test("onc>*!llklk[dd<dfd]");
> true

You may want to rethink your regular expression though, because as it stands, a single "]" character will pass the test:

re.test("]");
> true
Sign up to request clarification or add additional context in comments.

Comments

2
\S+\s*\[\S+?\]

You can try this if you want to allow special characters as well.

http://regex101.com/r/yA1jY6/3

4 Comments

Thanks @vks, this is wonderful. It passed series of test cases expect /\S+\s*[\S+?]/.test(" sdsd! sdsd [hjhj* sfsd] ") and /\S+\s*[\S+?]/.test(" sdsd! sdsd [hjhj*sfsd ] "). Please can you help me that?
Sorry I mean this is wonderful. It passed series of test cases expect /\S+\s*[\S+?]/.test(" sdsd! sdsd [] "). Please can you help me that? It passed without bbb and /\S+\s*[\S+?]/.test(" [] ") also passed
@Paullo This will change the question entirely and the answer.I suggest you post a new question with all requirements.Leave this accepted :)
I appreciate your efforts @vks but I think lechariotdor have close to what I am looking for
1

First, to make it easier to read, you could replace {0,} by * as it's the same thing. Next, \w would not match some symbols like > or *, you can use a . to match any symbol. Then, like an other answer is saying, you're expecting a space between the two groups (aaa and [bbb]), so your example won't match.

I think this regex is a good starting point (depending on your other requirements).

/.+\[.+\]/

Try it here

4 Comments

Thanks @lechariotdor I tested your solution, it seems okay but fail on this test /.*[.*]/.test("[dd<dfd!]"). It returned true instead of false
Ok, so if you need at least 1 character before the [bbb] group, you should replace the first * by a +. I edited. If you need at least one character between the [] too, you should replace the second * too.
Thanks once again for your effort but latest solution fails this test /.+[.*]/.test(" sdsd! sdsd [] "). This should return false
Hi bro please can you edit your solution to read /.+[.+]/ instead of /.+[.*]/. /.+[.+]/ is the actually the best solution so far

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.