2

I'm trying to execute regex replace after match char, example 3674802/3 or 637884-ORG

The id can become one of them, in that case, how can I use regex replace to match to remove after the match?

Input var id = 3674802/3 or 637884-ORG;

Expected Output 3674802 or 637884

Gauthaman Sahadevan
  • 923
  • 1
  • 11
  • 19
Harish
  • 189
  • 2
  • 13

5 Answers5

3

You could use sbustring method to take part of string only till '/' OR '-':

var input = "3674802/3";
var output = input.substr(0, input.indexOf('/')); 

var input = "637884-ORG";
var output = input.substr(0, input.indexOf('-')); 

    var input = "3674802/3";
    if (input.indexOf('/') > -1)
    {
      input = input.substr(0, input.indexOf('/')); 
    }
    console.log(input);
    
    var input = "637884-ORG";
    if (input.indexOf('-') > -1)
    {
      input = input.substr(0, input.indexOf('-'));
    }
    console.log(input);
Sahil Sharma
  • 3,847
  • 6
  • 48
  • 98
1

You can use a regex with a lookahead assertion

/(\d+)(?=[/-])/g

var id = "3674802/3"
console.log((id.match(/(\d+)(?=[/-])/g) || []).pop())
id = "637884-ORG"
console.log((id.match(/(\d+)(?=[/-])/g) || []).pop())
Ele
  • 33,468
  • 7
  • 37
  • 75
0

You don't need Regex for this. Regex is far more powerful than what you need.

You get away with the String's substring and indexOf methods.

indexOf takes in a character/substring and returns an integer. The integer represents what character position the character/substring starts at.

substring takes in a starting position and ending position, and returns the new string from the start to the end.

If are having trouble getting these to work; then, feel free to ask for more clarification.

Tyler
  • 957
  • 11
  • 27
  • Can you provide a snippet or sample code to backup your logic? – ctwheels Mar 28 '18 at 18:25
  • @ctwheels As Barmar had said: StackOverflow is not a free coding service. SO expects you to try to solve your own problem first. I intentionally left out a code example; because, I do not see the OP's effort to solve his own problem. I am pointing him in a strong direction to help him solve his problem. – Tyler Mar 28 '18 at 18:26
  • 1
    I understand, but to make your answer complete and according to StackOverflow's [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) article in the Help Center, you should `Write to the best of your ability`. You're failing to do that. It's one thing to *punish* the OP for not doing their homework, but it's another to punish future readers who might be looking for an answer to a similar question. Point is: Write an appropriate answer. If your intention was to sway the OP in a direction away from regex, this should be a comment. As it stands, this is not an answer. – ctwheels Mar 28 '18 at 18:28
  • @ctwheels "Write to the best of your ability" is talking about grammar, spelling and punctuation. Future readers and the OP should be able to managed with this answer. I have given the documentation and a brief description of two functions that can solve the problem. That is sufficient to solve the problem. If for some reason, the OP or future readers is still having trouble, as I had stated, he/she can "ask for more clarification". – Tyler Mar 28 '18 at 18:33
  • Well then this question should (according to your view on it) be unanswered: Under `Answer well-asked questions`: Not all questions can or should be answered here – ctwheels Mar 28 '18 at 18:35
  • @ctwheels On the topic of "intention was to sway the OP in a direction away from regex. As the "Answer the question" section in states: The answer can be “don’t do that”, but it should also include “try this instead”. – Tyler Mar 28 '18 at 18:35
  • Yes, I agree that you've presented that, but you don't show how to use your logic. **All I'm trying to say is to add more effort into your answer**. I think your solution is a good one, but you need to help prove that. Like I said, don't punish future readers for the OP's lack of information. Look at [Maverick's answer](https://stackoverflow.com/a/49541758/3600709), which has been upvoted. It provides a clear use case and makes his answer much more complete than yours. – ctwheels Mar 28 '18 at 18:38
  • @ctwheels Why should I expect future readers to be unable to take the resources I have provided and find the solution? If they are having trouble they can comment on my answer. – Tyler Mar 28 '18 at 18:40
  • #WhenSomeoneGetsDefensiveFromPositiveReinforcement – ctwheels Mar 28 '18 at 18:42
0

You can use the following script:

var str = '3674802/3 or 637884-ORG';
var id = str.replace(/(\d+)[-\/](?:\d+|[A-Z]+)/g, '$1');

Details concerning the regex:

  • (\d+) - A seuence of digits, the 1st capturing group.
  • [-\/] - Either a minus or a slash. Because / are regex delimiters, it must be escaped with a backslash.
  • (?: - Start of a non-capturing group, a "container" for alternatives.
  • \d+ - First alternative - a sequence of digits.
  • | - Alternative separator.
  • [A-Z]+ - Second alternative - a sequence of letters.
  • ) - End of the non-capturing group.
  • g - global option.

The expression to replace with: $1 - replace the whole finding with the first capturing group.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
0

Thanks To everyone who responded to my question, was really helpful to resolve my issue.

Here is My answer that I built:

var str = ['8484683*ORG','7488575/2','647658-ORG'];
    for(i=0;i<str.length;i++){
    var regRep = /((\/\/[^\/]+)?\/.*)|(\-.*)|(\*.*)/;
    var txt = str[i].replace(regRep,"");
    console.log(txt);
    }
Harish
  • 189
  • 2
  • 13