0

I have a dilemma in using regex as I am very new in using this:

I have the URL below:

var url = https://website.com/something-here/page.html?p=null#confirmation?order=123

My expected result is:

/something-here/page.html #confirmation

It could be a space or a comma or simply combine the two(/something-here/page.html#confirmation)

I can do this using two regex below:

var a= url.match(/som([^#]+).html/)[0];

var b= url.match(/#([^#]+).tion/)[0];

console.log(a,b);

But I would like to have it done as a single regex with the same result.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    Why a single regExp, if two regExps do what you need? – Teemu Nov 17 '17 at 21:58
  • 1. What is the purpose of your regex your final aim; the use. 2. What is that space for? – Bekim Bacaj Nov 17 '17 at 22:21
  • I would like to try to combine them or at least create a single regEx as a single field on a form I'm trying to create *should* only accept one-liner regex. :( I tried this format ^(?=.*some)(?=.*#) but it doesn't work. The space is just to separate the two but its not really a factor. I mean, they can be together with a comma or be combined as a one word. – Lea Sanchez Nov 17 '17 at 22:34
  • @LeaSanchez OK, see my update on the post. You have a *one-liner* regexp there. – Bekim Bacaj Nov 17 '17 at 22:54
  • Hi @Bekim Bacaj, your suggestion works properly. Thank you for your help. :) – Lea Sanchez Nov 17 '17 at 23:05
  • What about [`^https?:\/{2}[^\/]*(\/[^?]*)[^#]*(#[^?]*).*$` replace with `$1 $2`](https://regex101.com/r/A7c7UA/1)? – ctwheels Nov 18 '17 at 00:13

3 Answers3

0

You can use RegExp's group system to your advantage. Here's a snippet:

var matches = url.match(/(som[^#]+.html).*?(#[^#]+.tion)/);
console.log(matches[1] + " " + matches[2]); // prints /something-here/page.html #confirmation

I combined your two RegExp conditions into one, while enclosing them with parenthesis in the correct areas to create two groups.

That way, you can get the specified group and add a space in between.

Tals
  • 468
  • 4
  • 17
  • Hello @Tals, I tried to run it but it gives me an unexpected error due to the array for matches. When I remove them, it gives me a null value. I have also tried to replace the value with [0] and [1] respectively but still the same unexpected error still occurs. Thanks for this suggestion as well. :) – Lea Sanchez Nov 17 '17 at 22:53
  • 1
    Hi Tals, its a mistake on my end, I'm running it on the other sheet that I'm using for test haha. My apologies. This is correct and running. – Lea Sanchez Nov 17 '17 at 23:00
  • Cool! Good luck :) – Tals Nov 17 '17 at 23:42
0

Aside the fact that your example url is malformed (you have two search params), therefore not very suitable to work with - I have e proposition:

Why not use the URL object and its properties?

url = new URL("https://website.com/something-here/page.html?p=null#confirmation?order=123");

and precisely grab the properties with explicit syntax as in:

url.pathname; >> "something-here/page.html"

url.hash; >> "#confirmation?order=123"

But in case you explicitly need a RegExp variant

here is one

var url = "https://website.com/something-here/page.html?p=null#confirmation?order=123";
var match =  url.match(/\/som.*?html|\#.*?tion/g);

console.log(match.join(" "));
Bekim Bacaj
  • 5,707
  • 2
  • 24
  • 26
  • Hi Bekim, tahnk you so much for this. I was able to run it and it now works. Thank you so much for your help! ^_^ – Lea Sanchez Nov 17 '17 at 23:04
  • @LeaSanchez Great, (now you should mark the thread as solved, e.g. accept the answer that works best for you ). :) – Bekim Bacaj Nov 17 '17 at 23:09
-1

Use each your condition in scope "( )" More details answer try find here

Skytrace
  • 58
  • 4