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 %
Related
I have this string: 2015-07-023. I want to get 07 from this string.
I used RegExp like this
var regExp = /\(([^)]+-)\)/;
var matches = regExp.exec(id);
console.log(matches);
But I get null as output.
Any idea is appreciated on how to properly configure the RegExp.
The best way to do it is to not use RegEx at all, you can use regular JavaScript string methods:
var id_parts = id.split('-');
alert(id_parts[1]);
JavaScript string methods is often better than RegEx because it is faster, and it is more straight-forward and readable. Any programmer can read this code and quickly know that is is splitting the string into parts from id, and then getting the item at index 1
If you want regex, you can use following regex. Otherwise, it's better to go with string methods as in the answer by #vihan1086.
var str = '2015-07-023';
var matches = str.match(/-(\d+)-/)[1];
document.write(matches);
Regex Explanation
-: matches - literal
(): Capturing group
\d+: Matches one or more digits
Regex Visualization
EDIT
You can also use substr as follow, if the length of the required substring is fixed.
var str = '2015-07-023';
var newStr = str.substr(str.indexOf('-') + 1, 2);
document.write(newStr);
You may try the below positive lookahead based regex.
var string = "2015-07-02";
alert(string.match(/[^-]+(?=-[^-]*$)/))
I am relatively new to RegEx and am trying to achieve something which I think may be quite simple for someone more experienced than I.
I would like to construct a snippet in JavaScript which will take an input and strip anything before and including a specific character - in this case, an underscore.
Thus 0_test, 1_anotherTest, 2_someOtherTest would become test, anotherTest and someOtherTest, respectively.
Thanks in advance!
You can use the following regex (which can only be great if your special character is not known, see Alex's solution for just _):
^[^_]*_
Explanation:
^ - Beginning of a string
[^_]* - Any number of characters other than _
_ - Underscore
And replace with empty string.
var re = /^[^_]*_/;
var str = '1_anotherTest';
var subst = '';
document.getElementById("res").innerHTML = result = str.replace(re, subst);
<div id="res"/>
If you have to match before a digit, and you do not know which digit it can be, then the regex way is better (with the /^[^0-9]*[0-9]/ or /^\D*\d/ regex).
Simply read from its position to the end:
var str = "2_someOtherTest";
var res = str.substr(str.indexOf('_') + 1);
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+\$/
Say I had a string in JavaScript that looked like this:
var str = "Item%5B9%5D.Something%5B0%5D.Prop1=1&Item%5B9%5D.Something%5B0%5D.Prop2=False&Item%5B9%5D.Something%5B0%5D.Prop3=10%2F04%2F2013+00%3A00%3A00&Item%5B9%5D.Something%5B1%5D.Prop1=2&Item%5B9%5D.Something%5B1%5D.Prop2=False&Item%5B9%5D.Something%5B1%5D.Prop3=10%2F04%2F2013+00%3A00%3A00&Item%5B9%5D.Something%5B2%5D.Prop1=3&Item%5B9%5D.Something%5B2%5D.Prop2=False&Item%5B9%5D.Something%5B2%5D.Prop3=29%2F04%2F2013+00%3A00%3A00&Item%5B9%5D.Something%5B3%5D.Prop1=4&Item%5B9%5D.Something%5B3%5D.Prop2=False&Item%5B9%5D.Something%5B3%5D.Prop3=29%2F04%2F2013+00%3A00%3A00"
and wanted it to look like this:
var str = "Something%5B0%5D.Prop1=1&Something%5B0%5D.Prop2=False&Something%5B0%5D.Prop3=10%2F04%2F2013+00%3A00%3A00&Something%5B1%5D.Prop1=2&Something%5B1%5D.Prop2=False&Something%5B1%5D.Prop3=10%2F04%2F2013+00%3A00%3A00&Something%5B2%5D.Prop1=3&Something%5B2%5D.Prop2=False&Something%5B2%5D.Prop3=29%2F04%2F2013+00%3A00%3A00&Something%5B3%5D.Prop1=4&Something%5B3%5D.Prop2=False&Something%5B3%5D.Prop3=29%2F04%2F2013+00%3A00%3A00"
i.e. remove all of the Item%5BX%5D. parts
How would I go about doing this? I thought of using something like:
str = str.substring(str.indexOf('Something'), str.length);
but obviously that only removes the first occurrence.
Also the number in-between the %5B and %5D could be anything, not necessarily 9.
This seems like something that should be simple but for some reason I'm stumped. I found a few similarish things on SO but nothing that handled all the above criteria.
You could use a regular expression :
str = str.replace(/Item[^.]+\./g, '');
or if you want something more precise because you'd want to keep Item%6B3%4D :
str = str.replace(/Item%5B.%5D\./g, '');
str = str.replace('Item%5B9%5D', '');
EDIT: Missed the part where 9 in the string could be any number. You can use:
str = str.replace(/Item%5B\d%5D\./g, '');
Avoid using a regular expression where complex "needle" escaping is required:
var str = "something complex full of http://, 'quotes' and more keep1 something complex full of http://, 'quotes' and more keep2 something complex full of http://, 'quotes' and more keep3"
var needle = "something complex full of http://, 'quotes' and more";
while( str.indexOf(needle) != '-1')
str = str.replace(needle,"");
document.write(str);
Outputs:
keep1 keep2 keep3
Here you go:
str = str.replace(/Item%5B\d%5D\./g,'');
Live Demo
Try using regular expressions:
str = str.replace(/Item%5B[^.]*%5D./g, '');
This assumes that you can have anything of any length between %5B and %5D.
JSFiddle
Using split() & join() method
var str = "Item%5B9%5D.Something%5B0%5D.Prop1=1&Item%5B9%5D.Something%5B0%5D.Prop2=False&Item%5B9%5D.Something%5B0%5D.Prop3=10%2F04%2F2013+00%3A00%3A00&Item%5B9%5D.Something%5B1%5D.Prop1=2&Item%5B9%5D.Something%5B1%5D.Prop2=False&Item%5B9%5D.Something%5B1%5D.Prop3=10%2F04%2F2013+00%3A00%3A00&Item%5B9%5D.Something%5B2%5D.Prop1=3&Item%5B9%5D.Something%5B2%5D.Prop2=False&Item%5B9%5D.Something%5B2%5D.Prop3=29%2F04%2F2013+00%3A00%3A00&Item%5B9%5D.Something%5B3%5D.Prop1=4&Item%5B9%5D.Something%5B3%5D.Prop2=False&Item%5B9%5D.Something%5B3%5D.Prop3=29%2F04%2F2013+00%3A00%3A00";
console.log(str.split(/Item%5B\d%5D\./g).join(''));
I have a string like (which is a shared path)
\\cnyc12p20005c\mkt$\\XYZ\
I need to replace all \\ with single slash so that I can display it in textbox. Since it's a shared path the starting \\ should not be removed. All others can be removed.
How can I achieve this in JavaScript?
You could do it like this:
var newStr = str.replace(/(.)\\{2}/, "$1\\");
Or this, if you don't like having boobs in your code:
var newStr = "\\" + str.split(/\\{1,2}/).join("\\");
You can use regular expression to achieve this:
var s = '\\\\cnyc12p20005c\\mkt$\\\\XYZ\\';
console.log(s.replace(/.\\\\/g, '\\')); //will output \\cnyc12p20005c\mkt$\XYZ\
Double backslashes are used because backslash is special character and need to be escaped.