10

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)

Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
bumpy
  • 111
  • 1
  • 1
  • 5

4 Answers4

11
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).

Community
  • 1
  • 1
Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
  • Nice to know that I could help. Ps. If you feel that your question has been adequately answered, please click the check mark next to this answer (or one of the others, if you prefer) to mark it as accepted. Thanks! – Ilmari Karonen Sep 26 '12 at 21:47
  • Why would you use the `?` modifier in your example? – Knu Oct 25 '16 at 20:10
  • @Knu: Because I kind of assumed that the OP wanted to remove the comma and everything up to the *next* slash, not everything up to the *last* slash in the string. The question doesn't actually specify what should happen if there are multiple slashes, but it makes more sense to me that way. Of course, if it's guaranteed that there can never be more than one slash in the string, then it won't make any difference. (Let's not even get into what should happen if there are multiple commas...) – Ilmari Karonen Oct 26 '16 at 00:21
3

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);
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1
return str.split(',')[1].split('/')[0];
Frederik.L
  • 5,522
  • 2
  • 29
  • 41
-1
var commaIndex=str.indexOf(',')
var slashIndex=str.indexOf('/')
var finalString=str.substring(commaIndex,slashIndex)

Hope it will work

Apurv
  • 309
  • 2
  • 10