Replace params in javascript - javascript

I tried a lot to replace the query parammeter using Javascript. But its not working. Can you please share any solutions to replace the parameter
Below is the example
console.log("www.test.com?x=a".replace(new RegExp(`${"x=a"}&?`),''));
the output i am getting is www.test.com? . Is there any way to replace ? and to get only www.test.com.

If you want to remove whatever comes from the question mark including it, try this instead:
console.log("www.test.com?x=a".split("?")[0]);
That way you get only what's before the question mark.
I hope that helps you out.

You can remove all query strings using the following regex:
\?(.*)
const url = "www.test.com?x=1&b=2"
console.log(url.replace(/\?(.*)/, ''));

You could brutally replace the '?x=a' string with the JavaScript replace function or, even better, you could split the string in two (based on the index of ?) with the JavaScript split function and take the first part, e.g.:
let str = 'www.test.com?x=a';
console.log(str.replace('?x=a', ''));
console.log(str.split('?')[0]);

Related

How do you sanitize a string to pass a regex?

I have a regex validation I need my string to pass.
/^[0-9a-zA-Z-]+$/
I want to create a function that sanitizes the string for it to pass the regex.
I thought of doing something like
string.replace(/^[0-9a-zA-Z-]+$/,"");
Except I need to invert the above regex.
I tried to look up how to invert a regex but nothing seems to show up.
Try this string.replace(/\W/g,""). Also check this web site i always use it to test regular expressions, it also has hints on the right bottom
Negate the collection using ^ inside the []
const str = `abc*รง%ABC&(/())12345=?`
const newString = str.replace(/[^0-9a-zA-Z-]/g,"");
console.log(newString)

REGEX to get ID ath the end of an URL without /

I'm trying to do a GreaseMonkey script and i'm stucked on a REGEX to get the ID of an article in URL.
I have URL like https://www.blabla.com/poney-2000-poneys-dance-bla,272317
And i want to use the ID at the end after the Comma : 272317
I tried this REGEX : (,([\d]+)) to avoid taking digit in the rest of the URL and it get me ,272317 but I want it without the comma at the begining.
Do you have an idea how i can improve my REGEX ?
Thanks a lot :)
Just need to remove that outer paran since you don't want to capture the ,
,(\d+)$
An alternative approach without regex:
var url = 'https://www.blabla.com/poney-2000-poneys-dance-bla,272317';
url.substring(url.lastIndexOf(',') + 1);
The regex would be (\d+)$ to lookup the digits in the end but it doesn't consider the comma.
Try this: (?!,)([0-9]+){5}, it should be fine as long as the id is greater than 5

str replace all in Javascript

I am trying to some some urls throught javascript where some replacement of urls needs to be done. I have a textarea with some URLs example given below:
http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=1
http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=2
http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=3
http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=3
Now what i am trying to do is replacing http://mywebsite.com/preview.aspx?mode=desktop&url= with spaces.
I have tried using str.replace() but it is replacing only first occurence of that url.
I have also tried with Global variable g the query i have used is
str_replace(\http://mywebsite.com/preview.aspx?mode=desktop&url=/g,'');
But its not working So can anyone tell me how i can do that ?
I want the output of the textarea like:
http://mywebsite.com/post.aspx?id=44&content=1
http://mywebsite.com/post.aspx?id=44&content=2
http://mywebsite.com/post.aspx?id=44&content=3
http://mywebsite.com/post.aspx?id=44&content=4
I believe that your biggest issue is that your regex syntax is incorrect. Try this:
Imagine that var s is equal the the value of your textarea.
s.replace(/http\:\/\/mywebsite\.com\/preview.aspx\?mode\=desktop\&url\=/g, '');
The issue you were having was improper delimiters and unescaped reserved symbols.
Though Javascript has some of its own regex idiosyncrasies, the issues here were related to basic regex, you might find these resources useful:
http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
http://regexpal.com
try this.
var string = document.getElementById('textareaidhere');
string.replace(/http:\/\/mywebsite\.com\/preview\.aspxmode=desktop&url=/g, '');
JSFiddle here

regular expression with javascript for second match

I have an complicated string. From this string I like to get only the following marked part: (second match)
Example:
1;#FirstName,,
Surname,#Domain\Account,#email.#company.com,#email#company.com,#FirstName,,
Surname25;#FirstName,,
Surname,#Domain\Account,#email.#company.com,#email#company.com,#FirstName,, Surname26;#Helpdesk,#DE\helpdesk,#helpdesk#vega.com,#,#Helpdesk30;#...
I only want to get the Second "FirstName,,Surname" combination...
Any ideas how I could to this?
In the example above I need to ignore the complete first part starting from 1;# to 25;#
And then I need the "FirstName,,Surname" and after that the rest of the string should be ignored.
The numbers can be different and also the length from the string...
I started with this but it is not working:
((.*?[0-9]+.*?){2})[0-9]+
Thanks in advance for your help.
you can use string split function and split by var arry = yourstring.split(',') arry[9],arry[11] will contain your string.
Try the following regex:
(?:.+?;){2}#(.+?),#
(very weird string by the way)
You can see it working here: http://regex101.com/r/dV1lW5 and here: http://regexr.com?33rn5

JavaScript Regex expression to extract data from html comment

There is a html comment with an Id that I need to extract. The comment is on a div, which is not hard to get using the JQuery $ operator. But the correct RegEx string I need I have not been able to figure out. This is the comment:
<!-- sid=FFKK12H1 -->
And I need a JS variable that has the string "FFKK12H1" assigned to. What is the correct syntax/expression to use? thanks!
EDIT:
I forgot a very important piece of information: The code needs to work on IE7. Unfortunately this is the browser my company allows us to use, and none of the proposed solutions work there so far. Any other thoughs?
The regular expression would be: /<!-- sid=(.+?) -->/i:
var str = '<!-- sid=FFKK12H1 -->';
console.log(str.match(/<!-- sid=(.+?) -->/i)[1]);
var content = $('#comment-containg-div').html();
var regex = /<!--\s*sid=([\x00-\x7F]+)\s*-->/;
var matches = regex.exec(content);
console.log(matches);
The regex here is a amalgamted answer that includes all of the suggestions that other people on the page have made, it seems like it would be the safest to use.
var my_id = my_string.replace(/.*<!-- sid=(.*) -->.*/gi, '$1');
Example
http://jsfiddle.net/YTdKQ/

Categories