I'm not using REGEX very often so I don't know it well.
Want to match last digits before / end of string.
so my regex will be\d+/$
Now I want to replace matched part of href inside the link.
First thing
SyntaxError: illegal character
var regex = \d+/$
so I escaped it (I think) var regex = /\d+//$
I thought it will be simple from now:
$('a').attr('href').replace(regex,'00/')
But it seems no use.
I'm using firebug console for testing
Solution
url = "www.example.com/event/detail/46/"
var value = url.substring(url.lastIndexOf('/') + 1);
url = url.replace(value, '00')
What you seem to want is this :
$('a').attr('href', function(_,h){ return h.replace(/\d+\/$/,'00/') });
A slash is escaped as \/ in a regex literal, not as //.
$(selector).attr(name, fun) will apply the function to each element.
In escaping use \ not /.
So this will be
var regex = /\d+\$/
Related
I am totally new to regex. I need to grab particular set of string in href link dynamically (in this case is 1.867172) that I need to test.
<href = 'http://test.stage.cms.9c9media.net:8080/pete-test-1.867172'>
I don't think regex is necessary. A simple split could achieve this.
let URL = "http://test.stage.cms.9c9media.net:8080/pete-test-1.867172"
let testID = URL.split('-')[2]
console.log(testID)
Assuming that the number you want is always between a dash and the closing '
https://regexr.com/48s6u
If you want a regex solution. Please note this will capture any part of string of format <number>.<number>
var source = "http://test.stage.cms.9c9media.net:8080/pete-test-1.867172"
var regex = /\d\.\d+/;
var matches = source.match(regex);
if(matches)
console.log(matches[0])
Maybe you can use the next regular expression with a capturing group for all the characters following the last - char:
regular expression: /.*-(.*)/
Combine the previous regular expression with String.match to get what you looking for:
let link = document.getElementById("myLink");
let url = link.attributes.href.value;
let pattern = url.match(/.*-(.*)/)[1];
console.log("pattern: " + pattern);
<a id="myLink" href='http://test.stage.cms.9c9media.net:8080/pete-test-1.867172'></a>
I want to replace a text after a forward slash and before a end parantheses excluding the characters.
My text:
<h3>notThisText/IWantToReplaceThis)<h3>
$('h3').text($('h3').text().replace(regEx, 'textReplaced'));
Wanted result after replace:
notThisText/textReplaced)
I have tried
regex = /([^\/]+$)+/ //replaces the parantheses as well
regex = \/([^\)]+) //replaces the slash as well
but as you can see in my comments neither of these excludes both the slash and the end parantheses. Can someone help?
A pattern like /(?<=\/)[^)]+(?=\))/ won't work in JS as its regex engine does not support a lookbehind construct. So, you should use one of the following solutions:
s.replace(/(\/)[^)]+(\))/, '$1textReplaced$2')
s.replace(/(\/)[^)]+(?=\))/, '$1textReplaced')
s.replace(/(\/)[^)]+/, '$1textReplaced')
s.replace(/\/[^)]+\)/, '/textReplaced)')
The (...) forms a capturing group that can be referenced to with $ + number, a backreference, from the replacement pattern. The first solution is consuming / and ), and puts them into capturing groups. If you need to match consecutive, overlapping matches, use the second solution (s.replace(/(\/)[^)]+(?=\))/, '$1textReplaced')). If the ) is not required at the end, the third solution (replace(/(\/)[^)]+/, '$1textReplaced')) will do. The last solution (s.replace(/\/[^)]+\)/, '/textReplaced)')) will work if the / and ) are static values known beforehand.
You can use str.split('/')
var text = 'notThisText/IWantToReplaceThis';
var splited = text.split('/');
splited[1] = 'yourDesireText';
var output = splited.join('/');
console.log(output);
Try Following: In your case startChar='/', endChar = ')', origString=$('h3').text()
function customReplace(startChar, endChar, origString, replaceWith){
var strArray = origString.split(startChar);
return strArray[0] + startChar + replaceWith + endChar;
}
First of all, you didn't define clearly what is the format of the text which you want to replace and the non-replacement part. For example,
Does notThisText contain any slash /?
Does IWantToReplaceThis contain any parentheses )?
Since there are too many uncertainties, the answer here only shows up the pattern exactly matches your example:
yourText.replace(/(\/).*?(\))/g, '$1textReplaced$2')
var text = "notThisText/IWantToReplaceThis";
text = text.replace(/\/.*/, "/whatever");
output : "notThisText/whatever"`
I'm terrible with Regex, can't find a suitable answer on Stack which works for this.
I have a string like this:
var str = 'abc?def%3f999%3F^%&$*'
I only want to remove the following:
?, %3f and %3F (the entity codes for question marks)
I've tried this:
var theQuery = str.replace([\\?]|\\%3f|\\%3F,'');
But this doesn't appear to be valid regex. What's a solution that will work here?
You can use this:
var str = 'abc?def%3f999%3F^%&$*'
var theQuery = str.replace(/\?|%3f/gi, '');
//=> abcdef999^%&$*
You need to use regex delimiters / and /
You need to use global switch g
No need to double escape
No need to escape %
I would like to match a path in a Url, but ignoring the querystring.
The regex should include an optional trailing slash before the querystring.
Example urls that should give a valid match:
/path/?a=123&b=123
/path?a=123&b=123
So the string '/path' should match either of the above urls.
I have tried the following regex: (/path[^?]+).*
But this will only match urls like the first example above: /path/?a=123&b=123
Any idea how i would go about getting it to match the second example without the trailing slash as well?
Regex is a requirement.
No need for regexp:
url.split("?")[0];
If you really need it, then try this:
\/path\?*.*
EDIT Actually the most precise regexp should be:
^(\/path)(\/?\?{0}|\/?\?{1}.*)$
because you want to match either /path or /path/ or /path?something or /path/?something and nothing else. Note that ? means "at most one" while \? means a question mark.
BTW: What kind of routing library does not handle query strings?? I suggest using something else.
http://jsfiddle.net/bJcX3/
var re = /(\/?[^?]*?)\?.*/;
var p1 = "/path/to/something/?a=123&b=123";
var p2 = "/path/to/something/else?a=123&b=123";
var p1_matches = p1.match(re);
var p2_matches = p2.match(re);
document.write(p1_matches[1] + "<br>");
document.write(p2_matches[1] + "<br>");
Any working Regex to find image url ?
Example :
var reg = /^url\(|url\(".*"\)|\)$/;
var string = 'url("http://domain.com/randompath/random4509324041123213.jpg")';
var string2 = 'url(http://domain.com/randompath/random4509324041123213.jpg)';
console.log(string.match(reg));
console.log(string2.match(reg));
I tied but fail with this reg
pattern will look like this, I just want image url between url(" ") or url( )
I just want to get output like http://domain.com/randompath/random4509324041123213.jpg
http://jsbin.com/ahewaq/1/edit
I'd simply use this expression:
/url.*\("?([^")]+)/
This returns an array, where the first index (0) contains the entire match, the second will be the url itself, like so:
'url("http://domain.com/randompath/random4509324041123213.jpg")'.match(/url.*\("?([^")]+)/)[1];
//returns "http://domain.com/randompath/random4509324041123213.jpg"
//or without the quotes, same return, same expression
'url(http://domain.com/randompath/random4509324041123213.jpg)'.match(/url.*\("?([^")]+)/)[1];
If there is a change that single and double quotes are used, you can simply replace all " by either '" or ['"], in this case:
/url.*\(["']?([^"')]+)/
Try this regexp:
var regex = /\burl\(\"?(.*?)\"?\)/;
var match = regex.exec(string);
console.log(match[1]);
The URL is captured in the first subgroup.
If the string will always be consistent, one option would be simply to remove the first 4 characters url(" and the last two "):
var string = 'url("http://domain.com/randompath/random4509324041123213.jpg")';
// Remove last two characters
string = string.substr(0, string.length - 2);
// Remove first five characters
string = string.substr(5, string.length);
Here's a working fiddle.
Benefit of this approach: You can edit it yourself, without asking StackOverflow to do it for you. RegEx is great, but if you don't know it, peppering your code with it makes for a frustrating refactor.