2

I thought this regex was about turning words into an array, but I get null as a result

when I want to get the second word from "facebox otherword" with array[0]

This is the regex:

this.rel.match(/facebox\[?\.(\w+)\]?/)

Thanks Richard

1
  • Is your question about what the regex does or how to get the second word from "facebox otherword"? Commented Aug 10, 2011 at 14:21

8 Answers 8

7

http://xenon.stanford.edu/~xusch/regexp/analyzer.html is a simple regex analyzer (among many others). Since its not linkable, here is just a screenshot and the link:

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

There is actually a link at the bottom to "send to a friend": xenon.stanford.edu/~xusch/regexp/… . Looks like a great tool though it is strange the way it sometimes cuts up bits of text.
@Chris, thanks, that's me and my attention span, and my expectation to see a "Link to this" somewhere in large font at the top of the site.
@miku: I'd given you the benefit of the doubt and assumed it was just a new feature since you first start using the site so you hadn't noticed it. ;-)
5

I believe you are looking for is the split() function:

What you mentioned about turning words into an array is typically referred to as a split, so if you wanted to get "otherword" from "facebox otherword", you would do the following:

var test = "facebox otherword"
var word = test.split(' ')[1];

Working Demo

1 Comment

Thanks everyone, I get it now, really helpfull! I think I am going to use split in this case!
3

Your regex matches:

facebox.abc
facebox[.abc
facebox[.abc]
facebox.abc]

with abc being at least one "word character" (letters, digits, and underscores).

Comments

2

Note \.. That means that it is a dot, rather than any character. This regex cathches facebox.otherword

Comments

1

This matches text like:

facebox[.blabla]
facebox.blabla

Comments

0

The regex you have there won't match "facebook otherword", because of the \.. It's looking for a literal ".", so it would match the following: "facebook.otherword", "facebook[.otherword", "facebook.otherword]", or "facebook[.otherword]".

Comments

0

Checks that the string contained in the rel property of this

  1. contains the text 'facebox'
  2. followed by 0 or 1 open-square-brackets
  3. followed by a dot
  4. followed by 1 or more word characters
  5. followed by 0 or 1 close-square-brackets

If matched, the 1 or more word characters will be contained in a sub-match.

Comments

0

/facebox\[?\.(\w+)\]?/

  • matches facebox literal
  • then optionally match [
  • then match .
  • then match any word
  • then optionally match ]

Comments

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.