Regex Express Return All Chars before a '/' but if there are 2 '/' Return all before that

regex match everything until character
regex match before and after
regex tester
regex 3 characters before match
regex everything before comma
regex match before character
regex get everything before a comma
regex match until character or end of string

I have been trying to get a regex expression to return me the following in the following situations.

XX         ->   XX
XXX        ->   XXX
XX/XX      ->   XX
XX/XX/XX   ->   XX/XX
XXX/XXX/XX ->   XXX/XXX

I had the following Regex, however they do no work.

^[^/]+     => https://regex101.com/r/xvCbNB/1
=========
([A-Z])\w+ => https://regex101.com/r/xvCbNB/2

They are close but are not there.

Any Help would be appreciated.

Regular Expression to get all characters before "-", So I see many possibilities to achieve this. string text = "Foobar-test";. Regex Match everything till the first "-" Match result = Regex.Match(text� If all we wanted was to validate the password, we could stop right there. But if for any reason we also need to match and return the entire string—perhaps because we ran the regex on the output of a function and the password's characters haven't yet been assigned to a variable—we can easily do so now. Matching the Validated String

It will be easier to use a replace here to match / followed by non-slash characters before end of line:

Search regex:

/[^/]*$

Replacement String:

""

Updated RegEx Demo 1


If you're looking for a regex match then use this regex:

^(.*?)(?:/[^/]*)?$

Updated RegEx Demo 2

[PDF] Regular Expressions: The Complete Tutorial, A regular expression (regex or regexp for short) is a special text string for describing a search pattern. You your own regular expressions like you have never done anything else. the regex-directed engine will return the leftmost match. of the regex pattern before deciding that the regex cannot be matched with the text� The next column, "Legend", explains what the element means (or encodes) in the regex syntax. The next two columns work hand in hand: the "Example" column gives a valid regular expression that uses the element, and the "Sample Match" column presents a text string that could be matched by the regular expression.

Any special reason it has to be a regular expression? How about just splitting the string at the slashes, remove the last item and rejoin:

function removeItemAfterLastSlash(string) {
  const list = string.split(/\//);
  if (list.length == 1) [
     return string;
  } 

  list.pop();
  return list.join("/");
}

Or look for the last slash an remove it:

function removeItemAfterLastSlash(string) {
  const index = string.lastIndexOf("/");
  if (index === -1) {
    return string;
  }
  return string.splice(0, index);
}

Regular expressions, You construct a regular expression in one of two ways: characters, you'd use /\/ example\/[a-z]+/i —the backslashes before each slash make them literal. global search, looking in the whole string and returning all matches. The \K syntaxforces the regex engine to consider that any matched regex, before the \K form, is forgotten and that the final regex to match is, ONLY, the regex, located after the \K form. IMPORTANT: Due to the \K feature, included in the second S/R, you must use the Replace All button, exclusively.

How to remove portion of a string after certain character in , It specifies the character, or the regular expression, to use for splitting the string. If not used, the Return value: Returns a new array, having the splitted items. JavaScript String filter_none. Output: Before clicking on the button: How to remove all classes that begin with a certain string in JavaScript? How to remove all� Solution: We have to match only the lines that have a space between the list number and 'abc'. We can do that by using the expression \d\.\s+abc to match the number, the actual period (which must be escaped), one or more whitespace characters then the text.

Regular Expression HOWTO — Python 3.8.5 documentation, If the regex pattern is a string, \w will match all the characters marked as letters in the findall() has to create the entire list before it can be returned as the result. The regular expression should find and return everything EXCEPT the text string in the search expression. For example, if the word fox was what I wanted to exclude, and the searched text was: The quick brown fox jumped over the lazy dog.

Documentation: 9.3: Pattern Matching, The LIKE expression returns true if the string matches the supplied pattern. If you need parentheses in the pattern before the subexpression you want to extract, The function can return no rows, one row, or multiple rows (see the g flag below). they can override the case-sensitivity behavior implied by a regex operator,� Regular Expression to . Character classes. any character except newline \w \d \s: word, digit, whitespace

Comments
  • this was for an Oracle SQL Query so JavaScript was no good here, but good solution for JavaScript!