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))
Related
Having a string such like this one:
var str = "https://www.portalinmobiliario.com/venta/departamento/vitacura-metropolitana/6579-parque-arboleda-nva";
I need to be able to only get "6579" number from that string, because it is an ID I need to use. In fact,
this ID is always located between that last "/" and the next "-" that follows.
So considering that the url is dynamic but this "ID" is always between these two characters "/" and "-", I have to be able to always get the ID.
Any Idea how could I solve this?
I tried with this but
var id = str.lastIndexOf('/');
You can use a regular expression to match digits, followed by a dash and making sure anything after it is not a slash.
var str = "https://www.portalinmobiliario.com/venta/departamento/vitacura-metropolitana/6579-parque-arboleda-nva";
console.log(str.match(/\/(\d+)-[^/]+$/)[1])
If you do not want to use a regexp.
'"https://www.portalinmobiliario.com/venta/departamento/vitacura-metropolitana/6579-parque-arboleda-nva"'
.split('/').pop()
.split('-').shift();
pop() removes the last element.
shift() removes the first element.
Take your var id and substr your original String
var index = str.lastIndexOf('/');
var id = str.substr(index+1,4);
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));
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
I am trying to parse a webpage and to get the number reference after <li>YM#. For example I need to get 1234-234234 in a variable from the HTML that contains
<li>YM# 1234-234234 </li>
Many thanks for your help someone!
Rich
currently, your regex only matches if there is a single number before the dash and a single number after it. This will let you get one or more numbers in each place instead:
/YM#[0-9]+-[0-9]+/g
Then, you also need to capture it, so we use a cgroup to captue it:
/YM#([0-9]+-[0-9]+)/g
Then we need to refer to the capture group again, so we use the following code instead of the String.match
var regex = /YM#([0-9]+-[0-9]+)/g;
var match = regex.exec(text);
var id = match[1];
// 0: match of entire regex
// after that, each of the groups gets a number
(?!<li>YM#\s)([\d-]+)
http://regexr.com?30ng5
This will match the numbers.
Try this:
(<li>[^#<>]*?# *)([\d\-]+)\b
and get the result in $2.