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

javascript - Regex may contain a character - Stack Overflow

programmeradmin7浏览0评论

I have 2 kinds of data.

One data is something like this

ws=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9

and the another data like this

ws=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9;utmz=111872281.1437151704.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);

I wanna get the ws value, like

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9

But i got problems for split the ";" character. Because sometime the data have the character, but sometimes it doesn't have that character.

I already tried using

ws=([^]*);?

and

ws=([^]*)[;?]

and I still doesn't get the correct data. Thank you very much.

I have 2 kinds of data.

One data is something like this

ws=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9

and the another data like this

ws=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9;utmz=111872281.1437151704.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);

I wanna get the ws value, like

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9

But i got problems for split the ";" character. Because sometime the data have the character, but sometimes it doesn't have that character.

I already tried using

ws=([^]*);?

and

ws=([^]*)[;?]

and I still doesn't get the correct data. Thank you very much.

Share Improve this question asked Jul 25, 2015 at 11:11 user2565496user2565496 251 gold badge1 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

You can use:

/\bws=([^;]*)/

and grab captured group #1

RegEx Demo

([^;]*) will match 0 or more of any character that is not ;

I would use this:

ws=(.*?)(;|$)

Try it online: http://regexr./3bfbv

  • ws= searches for that exact text
  • ( starts the matching group
  • .*? searches for any characters, but as few characters as possbile ("non-greedy")
  • ) stops the matching group
  • (;|$) searches for either a ; or the end of the text (or the line)
发布评论

评论列表(0)

  1. 暂无评论