Currently I have the following structure (the ID at the end changes based on the code given by the shortner)
example.com/7NGOWX
example.com/7iTAXM
With javascript I am current using this code to change the url but it leaves the id.
<script>
document.body.innerHTML = document.body.innerHTML.replace(/example.com/g, '');
</script>
How can I make it so that it removes the entire url instead of leaving things like 7NGOWX 7iTAXM behind?
You need to use a little regex syntax:
example\.com\/\w+
\w+ matches any word character (equal to [a-zA-Z0-9_])
<script>
document.body.innerHTML = document.body.innerHTML.replace(/example\.com\/\w+/g, '');
</script>
You can use regex like /example\.com\/\w+/ , \w+ match any alphanumerical continuous word.
Another option can be to use the URL() constructor and have it do the work.
for example
var url = new URL("http://example.com/7iTAXM");
console.log(url.pathname.substring(1));
Related
Can someone please help in splitting an ID after a specific word in a URL. I need to delete a specific ID from URL and insert a custom ID.
The url goes like : "/abc/mode/1234aqwer/mode1?query".
I need to replace 1234qwer by 3456asdf.
Example:
Input:
/abc/mode/1234aqwer/mode1?query
Output:
/abc/mode/3456asdf/mode1?query
One option is to .replace /mode/ followed by non-slash characters, with /mode/ and your desired replacement string:
const input = '/abc/mode/1234aqwer/mode1?query';
console.log(
input.replace(/\/mode\/[^\/]+/, '/mode/3456asdf')
);
This is the solution without using regex. Use ES6's replace instead:
url = "/abc/mode/1234aqwer/mode1?query"
replace = "1234aqwer"
replaceWith = "3456asdf"
console.log(url.replace(replace, replaceWith))
I have regex to parse all hash url in HTML content.
/(\#)([^\s]+")/g
HTML content will be as
Some text some linksome content some link1
Expected is
#some-hash1, #some-hash2
But current regex is returning as (ending double come along with hash):
#some-hash1", #some-hash2"
I am unable to understand why its come along with double quotes. Any suggestion that will be very helpful.
I wouldn't use regex for this because it's overkill and because you can simply loop through the anchors pulling the value of their hrefs...
var anchors = document.querySelectorAll('a');
var hrefs = [];
anchors.forEach(function(e){
hrefs.push(e.getAttribute('href'));
});
console.log(hrefs);
link 1
link 2
Use non-capturing parenthesis,
/(\#)([^\s]+(?="))/g
DEMO
var z = 'Some text some linksome content some link1';
console.log( z.match(/(\#)([^\s]+(?="))/g) );
I am assuming that you are looking at the content of $2 for your result.
If so, the problem is the " inside the second capture group. Changing /(\#)([^\s]+")/g to /(\#)([^\s]+")/g results in the correct result.
I suggest joining the capture groups. Then /(\#[^\s]+)"/g will return $1=>#some-hash1, #some-hash2
Since $1 will always just return #, I suppose you trim it off elsewhere in your program, so perhaps you should use /\#([^\s]+)"/g which will return some-hash1, some-hash2 without the #
Just move double quote out the brackets:
(\#)([^\s]+)"
See how it works: https://regex101.com/r/fmrDyu/1
I am trying to replace a pattern as below:
Original :
welocme
Need to be replaced as :
welcome
Tried the below approach:
String text = "welocme";
Pattern linkPattern = Pattern.compile("a href=\"#");
text = linkPattern.matcher(text).replaceAll("a href=\"javascript:call()\"");
But not able to add the idvalue in between. Kindly help me out.
Thanks in advance.
how about a simple
text.replaceAll("#idvalue","javascript:call('idvalue')")
for this case only. If you are looking to do something more comprehensive, then as suggested in the other answer, an XML parser would be ideal.
Try getting the part that might change and you want to keep as a group, e.g. like this:
text = text.replaceAll( "href=\"#(.*?)\"", "href=\"javascript:call('$1')" );
This basically matches and replaces href="whatever" with whatever being caught by capturing group 1 and reinserted in the replacement string by using $1 as a reference to the content of group 1.
Note that applying regex to HTML and Javascript might be tricky (single or double quotes allowed, comments, nested elements etc.) so it might be better to use a html parser instead.
Add a capture group to the matcher regex and then reference the group in the replacemet. I found using the JavaDoc for Matcher, that you need to use '$' instead of '\' to access the capture group in the replacement.
Code:
String text = "welcome";
System.out.println("input: " + text);
Pattern linkPattern = Pattern.compile("a href=\"#([^\"]+)\"");
text = linkPattern.matcher(text).replaceAll("a href=\"javascript:call('$1')\"");
System.out.println("output: " +text);
Result:
input: welcome
output: welcome
I have a url like http://www.somedotcom.com/all/~childrens-day/pr?sid=all.
I want to extract childrens-day. How to get that? Right now I am doing it like this
url = "http://www.somedotcom.com/all/~childrens-day/pr?sid=all"
url.match('~.+\/');
But what I am getting is ["~childrens-day/"].
Is there a (definitely there would be) short and sweet way to get the above text without ["~ and /"] i.e just childrens-day.
Thanks
You could use a negated character class and a capture group ( ) and refer to capture group #1. The caret (^) inside of a character class [ ] is considered the negation operator.
var url = "http://www.somedotcom.com/all/~childrens-day/pr?sid=all";
var result = url.match(/~([^~]+)\//);
console.log(result[1]); // "childrens-day"
See Working demo
Note: If you have many url's inside of a string you may want to add the ? quantifier for a non greedy match.
var result = url.match(/~([^~]+?)\//);
Like so:
var url = "http://www.somedotcom.com/all/~childrens-day/pr?sid=all"
var matches = url.match(/~(.+?)\//);
console.log(matches[1]);
Working example: http://regex101.com/r/xU4nZ6
Note that your regular expression wasn't actually properly delimited either, not sure how you got the result you did.
Use non-capturing groups with a captured group then access the [1] element of the matches array:
(?:~)(.+)(?:/)
Keep in mind that you will need to escape your / if using it also as your RegEx delimiter.
Yes, it is.
url = "http://www.somedotcom.com/all/~childrens-day/pr?sid=all";
url.match('~(.+)\/')[1];
Just wrap what you need into parenteses group. No more modifications into your code is needed.
References: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
You could just do a string replace.
url.replace('~', '');
url.replace('/', '');
http://www.w3schools.com/jsref/jsref_replace.asp
In javascript I need to get the text within the custom tag. For example
[tag_retweet attr="val" attr2="val"]
In this case I need to get the word "retweet" only skipping all other texts and another example is,
[tag_share]
Here I need to get the word "share".
So what will be the regexp for getting that tag name in my case ??
Something like /\[tag_([a-z0-9_]+)(?:\s+|\])/
var tag = '[tag_retweet attr="val" attr2="val"]';
var match = tag.match(/\[tag_([a-z0-9_]+)(?:\s+|\])/);
window.alert(match[1]); // alerts "retweet"
The regex to capture it would be:
/.*\[tag_(.*?)\W.*/
This matches any characters up to the end of [tag_ and then starts capturing any caracters until it encounters a non-word character, then any other characters. The match will contain only the releavant parts.
use it like:
myString.match(/.*\[tag_(.*?)\W.*/)[1]
Basically, you're looking for what comes after [tag_, up until the next space (or the end of the tag)
So:
var tag = '[tag_retweet attr="val" attr2="val"]';
// or var tag = '[tag_share]';
var match = tag.match(/\[tag_(.*?)[\] ]/)[1];