Using Regex to pick up a certain value

Asked

Viewed 947 times

2

I am using this "string?" in regex ^\/(.*)\w+$ It detects values that start like this: /STRINGQUALQUER would like to catch the STRINGQUALQUER, what code do I use? I use jQuery.

  • 1

    What do you mean? What do you mean by "any string"? Your regex says that the first character is /, retire ^\/ then you don’t have to start with /. But what do you really want to do? Give examples and you’ll get a better answer and you don’t have to similar questions several times...

  • @Sergio Good afternoon to you! I did not ask similar questions haha, in the other question I needed to get the verification, and now I need to get a value, are of the same context but follow different paths. ANYWAY. I don’t know if Regex can do this, but I get values that way /stringqualquer, when I speak stringqualquer I mean any valid character, and I wish to obtain this value that comes after the /

  • Just a question. You’re trying to capture what in fact? .* will capture any character, but then you have \w capturing characters from A-Z and 0-9. .* will already work for the \w, I don’t understand what it is pro regex catch.

  • I also don’t understand how jQuery is related to the problem.

  • @Thomas in theory I wanted to take any character.

  • i want to capture value with jQuery.

  • Could you edit the question and put the part of the code that uses regex? It will help to give a better answer to your case and to be clear the use of jQuery in the problem.

  • @user3163662 ok! and wants only characters that come from a slash /? And you just want to pick strings that start with slash?

  • @user3163662 and by the way where will you use this regex? num .match(), .replace() or another?...

  • 1

    I managed to solve this problem?

Show 5 more comments

2 answers

3


The regex you have now can be divided into 5 parts to understand what you have and how it works:

  • ^ marks the start of the string.
  • \/ means the character /
  • (.*) capture group. Will catch all characters except new lines.
  • \w+ characters of words (letters, numbers and _)
  • $ end of string

If you want to get the content after / just use \/(.*). Then omit that the bar has to be at the beginning of the string but tells regex that there has to be a bar before the other characters in the string, using .*.

Example at this link.

2

Good from what I understand you have a string /stringqualquer and would like to get the value of stringqualquer without the / in front. Correct?

If that’s your problem it should solve:

[?<=\/](\w*)

or so:

\/?(\w*)

you can test online here.

My response was based on the response of @Renato Dinhani Conceição, who is here on SO EN.

I just switched the , for \/? and ([0-9]+) for (\w*) to meet your need.

  • 1

    I was almost finished writing my reply, I was going to suggest regex101 as well. + 1

  • Try to use it like this [?<=\/](\w*) or \/?(\w*)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.