I have this string
'bookmarkState={"params":{"date_from":"2014-07-31","date_to":"2014-10-01"}}'
I want to replace 2014-07-31 with 2014-01-01, i.e. the substring contained between '"date_from":"' and '","', using a regular expression in javascript. I have written this code but it doesn't work:
var qs = 'bookmarkState={"params":{"date_from":"2014-07-31","date_to":"2014-10-01"}};'
var regEx = /^(.*?date_from":")[^"]*(".*)$/;
qs = qs.replace(regEx, '2014-01-01');`
You don't need a regex to do that:
eval('bookmarkState={"params":{"date_from":"2014-07-31","date_to":"2014-10-01"}}');
bookmarkState.params.date_from = '1988-04-12';
console.log(JSON.stringify(bookmarkState));
^(.*?date_from":")[^"]*(",".*)$
Try this.Replace by $1<your string>$2.See demo.
http://regex101.com/r/qZ0uP0/1
Related
I have a string (url) like this:
https://8.random.url.com/g/DFGTER5675/test1/undefined/codec/
Here is my regex:
/https\:\/\/(?:.*)\/g\/(?:.*)\/(?:.*)\/(.*)\/codec\/(?:.*)/gi
And my code:
var string = "https://8.random.url.com/g/DFGTER5675/test1/undefined/codec/";
var myRegexp = /https\:\/\/(?:.*)\/g\/(?:.*)\/(?:.*)\/(.*)\/codec\/(?:.*)/gi
var match = string.replace(myRegexp, "OMEGA3");
When I do console.log(match) it returns only "OMEGA3". What I want is just my string with "undefined" replaced by "OMEGA3". What am I doing wrong? Thanks.
You can use this regex with capturing groups and back-reference:
url = url.replace(/(https?:\/\/[^\/]*\/g\/[^\/]*\/[^\/]*\/).*(\/codec\/)/gi, '$1OMEGA3$2');
RegEx Demo
You have the use of the capture group backwards. You should be capturing the parts of the pattern that you want to keep, not the part you want to replace. Then use $1, $2, etc. to copy those to the replacement.
You also have several non-capturing groups that aren't needed at all.
var myRegexp = /(https:\/\/.*\/g\/.*\/).*(\/codec\/)/gi
var match = string.replace(myRegexp, "$1OMEGA3$2");
why not use /undefined/gi
var string = "https://8.random.url.com/g/DFGTER5675/test1/undefined/codec/";
var myRegexp = /(https\:\/\/.*?\/.*?\/.*?\/.*?\/).*?(\/.*?\/)/gi
var match = string.replace(myRegexp, "$1OMEGA3$2");
console.log(match)
Is it possible to modify the group match output of a javascript regex
psudo code:
var from_another_script=/file:(\w+)/;
var inp = 'file:name';
var matches = inp.match(from_another_script);
normally output would be “name”
I would like to modify the regex to get output as “name_file”
I have seen some regex references (not js and not match) as
/file:(\w+)/$1_file
Not with match, but you can do this:
var inp = 'file:name';
var output = inp.replace(/file:(\w+)/, '$1_file');
What you had in another language wasn't just a regex : it was a regex AND a replacement pattern. You can't add _file with just a regex. If you can change the calling code, here's a solution:
var from_another_script={
regex: /file:(\w+)/,
replacementPattern: '$1_file'
}
var inp = 'file:name';
var output = inp.replace(from_another_script.regex, from_another_script.replacementPattern);
You can use the following:
inp.replace(/file:(\w+)/, '$1_file');
I'd like to get the talker name of some mp3s files paths such as the following:
/assets/audio/James_Lee/001.mp3
/assets/audio/Marc_Smith/001.mp3
/aasets/audio/blahblah/001.mp3
In the previous example we note that each talker name is surrounded by two slashes where the first of them is prefixed with the word audio. I need a pattern that matches names like the example above using javascript.
I tried at http://regexpal.com/ :
audio/.*/
but it only matches *audio/The_name/* where I need *The_name* only. The other thing I don't know how could I use such patterns with javascript replace().
This will get your the name: (?<=\/assets\/audio\/).*(?=\/)
Here's the regex in use: http://regexr.com?34747
Considering Javascript, you could do this:
var string = "/assets/audio/James_Lee/001.mp3";
var name = string.replace(/^.*\/audio\/|\/[\d]+\..*$/g, '');
Try this:
var str = "/assets/audio/James_Lee/001.mp3\n/assets/audio/Marc_Smith/001.mp3";
var pattern = /audio\/(.+?)\//g;
var match;
var matches = [];
while ((match = pattern.exec(str)) !== null){
matches.push(match[1]);
}
console.log(matches);
// If you want a string with only the names, you can re-combine the matches
str = matches.join('\n');
how about this?
str.replace(/.*audio\/([^\/]*)\/.*/,"$1")
I'm sure this is an easy one, but I can't find it on the net.
This code:
var new_html = "foo and bar(arg)";
var bad_string = "bar(arg)";
var regex = new RegExp(bad_string, "igm");
var bad_start = new_html.search(regex);
sets bad_start to -1 (not found). If I remove the (arg), it runs as expected (bad_start == 8). Is there something I can do to make the (very handy) "new Regexp" syntax work, or do I have to find another way? This example is trivial, but in the real app it would be doing global search and replace, so I need the regex and the "g". Or do I?
TIA
Escape the brackets by double back slashes \\. Try this.
var new_html = "foo and bar(arg)";
var bad_string = "bar\\(arg\\)";
var regex = new RegExp(bad_string, "igm");
var bad_start = new_html.search(regex);
Demo
Your RegEx definition string should be:
var bad_string = "bar\\(arg\\)";
Special characters need to be escaped when using RegEx, and because you are building the RegEx in a string you need to escape your escape character :P
http://www.regular-expressions.info/characters.html
You need to escape the special characters contained in string you are creating your Regex from. For example, define this function:
function escapeRegex(string) {
return string.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&');
}
And use it to assign the result to your bad_string variable:
let bad_string = "bar(arg)"
bad_string = escapeRegex(bad_string)
// You can now use the string to create the Regex :v:
I have a string like foobar1, foobaz2, barbar23, nobar100 I want only foobar, foobaz, barbar, nobar and ignoring the number part.
If you want to strip out things that are digits, a regex can do that for you:
var s = "foobar1";
s = s.replace(/\d/g, "");
alert(s);
// "foobar"
(\d is the regex class for "digit". We're replacing them with nothing.)
Note that as given, it will remove any digit anywhere in the string.
This can be done in JavaScript:
/^[^\d]+/.exec("foobar1")[0]
This will return all characters from the beginning of string until a number is found.
var str = 'foobar1, foobaz2, barbar23, nobar100';
console.log(str.replace(/\d/g, ''));
Find some more information about regular expressions in javascript...
This should do what you want:
var re = /[0-9]*/g;
var newvalue= oldvalue.replace(re,"");
This replaces al numbers in the entire string. If you only want to remove at the end then use this:
var re = /[0-9]*$/g;
I don't know how to do that in JQuery, but in JavaScript you can just use a regular expression string replace.
var yourString = "foobar1, foobaz2, barbar23, nobar100";
var yourStringMinusDigits = yourString.replace(/\d/g,"");