Regex with Javascript - Taking only part of the string

Asked

Viewed 1,540 times

1

Let’s say I have the following string:

str = "http://www.google.com/joao.pedro?a=b";

Then I want to take just the Joao. using Javascript.

I believe you can do with Regex somehow, someone can help me as?

I thought of something like, use this regular expression:

(http://www.google.com/)([\d\w\.]+)

And take the value that would be between the second parentheses. In PHP you can do something like this using the function preg_match, but and in Javascript?

  • I’ll edit the question.

  • I want to extract the name that is after . com/ and before ??... Anyway, I think I’m finding the solution here and already put in Stack.

  • 2

    See: https://jsbin.com/wesapo/edit?js,console

  • Yes, I used the match function instead of this, but the result is the same. See my answer, if I wrote something wrong please tell me to correct, or post one of yours that I will mark as the best answer, since it solves my case.

5 answers

1

Very simple and does not need regex:

var str = "http://www.google.com/joao.pedro?a=b/";
var valor= str.substring(str.lastIndexOf(".com/")+5,str.lastIndexOf("?"));

Upshot: joao.pedro

explanation ".com/" or only "/", the bar can appear at the end of the link and the codio would have to have validations to avoid or treat the string o +5 is to remove . com/ from the output

  • and if it is .com.br?

  • can be . tr, . fr, .es... this can be validated with contains ". br/" same thing needs to be done if you make use of regex

  • would number one by one? and end in ?a=com?

  • for the first question yes, for the second question: but already finish with that no? @Andrefigueiredo

1

In this case, for solution need to use the match function.

This function will return an array with regular expression occurrences.

In this application:

string = "http://www.google.com/joao.pedro?a=b";
a = string.match(/(http:\/\/www\.google\.com\/)([\d\w\.]+)/);

a returns an array:

Array [ "http://www.google.com/joao.pedro", "http://www.google.com/", "joao.pedro" ]

Note that the first occurrence is the whole body of the string, and the subsequent ones would be the occurrences that the regular expression found in the string.

0

const regex = /[a-z]\\/([\w.]+)/g;

const rege = (text) => {
let user = regex.exec(text);
 return hue[1];
}

console.log(
  rege("http://www.google.com/joao.pedro?a=b")
)

0

My regex was like this:

   /(\w+\.\w+)(?=\?)/
  1. (\w+\.\w+) Look for a text that has a . followed by a text.
  2. (?=\?) use Positive Lookahead, that is, looking forward look for ?

Running on regex101

0

A. Com Regexp

One among the infinite (ie: .* x Infinity) possibilities, a generic is:

str.match(/^.*(:\/\/)*.*\/(.*)[#\?].*/)[2]

which can be translated from the beginning (^), any string 0-"Infinite" (.*), except for and followed by (://) (aka protocol), character chair followed by / (domain), string (path - what we want), string started by ? (querystring) or # (hash Location).

B. Without Regexp

  1. Partitioning String - "one line version":

    (((str
      .split('://')[1] || '')
      .split('/')[1] || '')
      .split('?')[0] || '')
      .split('#')[0]
    
  2. Using HTMLHyperlinkElementUtils:

    var a = document.createElement('a')
    a.href="http://www.google.com/joao.pedro?a=b"
    a.pathname.substring(1)
    

* I am not taking into account other little-used URI parts on the Web, such as user and password.

Browser other questions tagged

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