0

My string have a two part and separated by /

I want left side string of slash accept any string except "HAHAHA" end of word And right side string of slash accept any string and allow use "HAHAHA" in end of string

only by Regular Expression and match function to return result parts

For example:

Accept : fooo/baarHAHAHA

Reject : fooHAHAHA/baaar

I want if string have one part, for example baarHAHAHA, accept but result like this:

string: baarHAHAHA

Group1: empty

Group2: baarHAHAHA

Have any idea?

7
  • Do any of the below answers help? Commented Jun 26, 2020 at 4:22
  • @Mandy8055 Yes. Thank you, lazy quantifier solved my problem Commented Jun 27, 2020 at 5:07
  • @mandy8055 I want use regex for any route pattern Commented Jun 27, 2020 at 6:20
  • No, For example : nutella.html reject and chocolate/nutella.html accept when part1(required)/part2(optional) Commented Jun 27, 2020 at 6:32
  • Does this help? Commented Jun 27, 2020 at 6:48

3 Answers 3

4

You can try

^(\w*?)(?<!HAHAHA)\/?(\w+)$

Explanation of the above regex:

  • ^, $ - Represents start and end of the line respectively.
  • (\w*?) - Represents first capturing group capturing the word characters([a-zA-Z0-9_]) zero or more times lazily.
  • (?<!HAHAHA) - Represents a negative look-behind not matching if the first captured group contains HAHAHA at the end.
  • \/? - Matches / literally zero or one time.
  • (\w+) - Represents second capturing group matching word characters([0-9a-zA-Z_]) one or more times.

Pictorial Representation

You can find the demo of the above regex in here.

const regex = /^(\w*?)(?<!HAHAHA)\/?(\w+)$/gm;
const str = `
fooo/baarHAHAHA
fooHAHAHA/baaar
/baar
barHAHAHA
`;
let m;
let resultString = "";
while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    if(m[1] === "")resultString = resultString.concat(`GROUP 1: empty\nGROUP 2: ${m[2]}\n`);
    else resultString = resultString.concat(`GROUP 1: ${m[1]}\nGROUP 2: ${m[2]}\n`);
}

console.log(resultString);

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, lazy quantifier solved my problem. But by this regex for pattern (part1/part2) part1 always is optional
It is true. But what if the first part is needed? this is new question
for example : baarHAHAHA reject and foo/baarHAHAHA accept ?
Yes, can you answer it?
Update my question.
0

You don't need regex for this, which is good since it is quite slow. A simple string.split() should be enough to separate the parts. Then you can just check if the word contains "HAHAHA" with the string.endsWith() method.

const a = 'fooHAHAHA/bar';
const b = 'foo/bar';
const c = 'fooHAHAHA';

console.log(a.split('/')); // Array [ 'fooHAHAHA', 'bar' ]
console.log(b.split('/')); // Array [ 'foo', 'bar' ]
console.log(c.split('/')); // Array [ 'fooHAHAHA' ]

// therefore ...
function splitMyString(str) {
    const strSplit = str.split('/');
    if (strSplit.length > 1) {
       if (strSplit[0].endsWith('HAHAHA')) {
           return ''; // or whatever you want to do if it gets rejected ...
       }
    }
    return str;
}

console.log('a: ', splitMyString(a)); // ''
console.log('b: ', splitMyString(b)); // foo/bar
console.log('c: ', splitMyString(c)); // fooHAHAHA

Alternative non-regex solution:

const a = 'fooHAHAHA/bar';
const b = 'foo/bar';
const c = 'fooHAHAHA';

function splitMyString(str) {
    const separator = str.indexOf('/');
    if (separator !== -1) {
      const firstPart = str.substring(0, separator);
      if (firstPart.endsWith('HAHAHA')) {
         return ''; // or whatever you want to do if it gets rejected ...
      }
    }
    return str;
}

console.log('a: ', splitMyString(a)); // ''
console.log('b: ', splitMyString(b)); // foo/bar
console.log('c: ', splitMyString(c)); // fooHAHAHA

1 Comment

I think it was some sort of exercise, soprobably regex was needed
0
var str, re;

function match(rgx, str) {
  this.str = str;
  this.patt = rgx
  var R = [], r;
  while (r = re.exec(str)) {
    R.push({
      "match": r[0],
      "groups": r.slice(1)
    })
  }
  return R;
}

str = `
fooo/baarHAHAHA
fooHAHAHA/baaar
/baar
barHAHAHA
barr/bhHAHAHA
`;

re = /(?<=\s|^)(.*?)\/(.*?HAHAHA)(?=\s)/g;

console.log(match(re, str))

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

Edit: When I make this code I think to letting user to call the str and when call it, it will return the mactheds and groups. But, if I make like this.str = str and have return too, this.str will be declined.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.