Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a substring between two specific characters

So I have a string:

"this is the beginning, this is what i want to remove/ and this is the end"

How do I use Javascript to target the string between the comma and the forward slash? (I also want to remove the comma and the slash)

like image 402
bumpy Avatar asked Sep 25 '12 11:09

bumpy


3 Answers

string = string.replace( /,.*?\//, '' );

In the regexp, , matches itself, .*? matches any sequence of characters (preferring the shortest match, due to the ? modifier) and \/ matches a slash. (The slash needs to be escaped with a backslash because it's also used as the regexp delimiter. Unfortunately, JavaScript doesn't support alternative regexp literal delimiters, so you'll just have to cope with the leaning toothpick syndrome.)

Note that the code above removes everything from the first comma to the first following slash in the string. Depending on what you want to happen if there might be multiple commas or slashes, there are various ways to modify the behavior of the regexp. For example:

  • .replace( /,.*\//, '' ) removes everything from the first comma to the last slash;
  • .replace( /^(.*),.*?\//, '$1' ) removes everything from the last comma followed by a slash up to the next slash;
  • .replace( /^(.*),.*\//, '$1' ) removes everything from the last comma followed by a slash up to the last slash in the string;
  • .replace( /,[^\/]*\//, '' ) does the same as the original regexp, i.e. removes everything from the first comma to the first following slash (even if there are other commas in between);
  • .replace( /,[^,\/]*\//, '' ) removes the first substring that begins with a comma and ends with a slash, and does not contain any commas or slashes in between;
  • .replace( /^(.*),[^,\/]*\//, '$1' ) removes the last substring that begins with a comma and ends with a slash, and has no commas or slashes in between; and
  • .replace( /^,[^,\/]*\//g, '' ) removes all substrings that begin with a comma and end with a slash, and have no commas or slashes in between.

Also note that, due to a historical quirk of regexp syntax, the . metacharacter will actually match any character except a newline ("\n"). Most regexp implementations provide some way to turn off this quirk and make . really match all characters, but JavaScript, for some reason, doesn't. If your string might contain newlines, you should replace all occurrences of . in the regexps above with some workaround such as [\s\S] (works in most PCRE-style regexp engines) or [^] (shorter, but specific to JavaScript's regexp flavor).

like image 196
Ilmari Karonen Avatar answered Sep 19 '22 20:09

Ilmari Karonen


As your question is tagged with regex, you seem to want .replace:

return str.replace(/, this is what i want to remove\//, "");

If you don't know the string in between, use

/,(.+?)\//

or with a * instead of the + if the string could also be empty

With string functions, it would be

var cpos = str.indexOf(","),
    spos = str.indexOf("/");
if (cpos > -1 && spos > cpos)
    return str.substr(0, cpos)+str.substr(spos+1);
like image 28
Bergi Avatar answered Sep 17 '22 20:09

Bergi


return str.split(',')[1].split('/')[0];
like image 28
Frederik.L Avatar answered Sep 19 '22 20:09

Frederik.L