最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript regex split for special characters - Stack Overflow

programmeradmin1浏览0评论

Have a look at this fiddle /

Here I want to split the characters which starts with [* or [*# and ends with *]. The current regular expression split the string which starts with [*# but not [*. I have tried the following patterns,

/(\[\*\#*[a-zA-Z0-9]+\*\])/g

/(\[\*\#{0,1}[a-zA-Z0-9]+\*\])/g

Thanks in advance.

Have a look at this fiddle http://jsfiddle/yeXWv/

Here I want to split the characters which starts with [* or [*# and ends with *]. The current regular expression split the string which starts with [*# but not [*. I have tried the following patterns,

/(\[\*\#*[a-zA-Z0-9]+\*\])/g

/(\[\*\#{0,1}[a-zA-Z0-9]+\*\])/g

Thanks in advance.

Share asked May 29, 2013 at 14:26 moustachemanmoustacheman 1,4644 gold badges23 silver badges47 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Try making the hash character optional:

/(\[\*\#?[a-zA-Z0-9 ]+\*\])/g

Edit: added missing white space :-)

You forgot to allow for spaces, which was the real problem -- not the missing # character.

/(\[\*\#?[a-zA-Z0-9 ]+\*\])/g

That will preserve the [*...*] strings in the output array. To omit them, remove the parentheses:

/\[\*\#?[a-zA-Z0-9 ]+\*\]/g

http://jsfiddle/mblase75/zU576/

发布评论

评论列表(0)

  1. 暂无评论