Using Flickr API.
result.items[i].published
Results in something like this 2012-12-21T14:36:24Z - notice the T and the Z?
How do I match only the numbers in 2012-12-21T14:36:24Z?
This is how it looks.
newPic.innerHTML = "<div class='pic-container'> <p class='title'>" + result.items[i].title + "</p> <a href="+ result.items[i].link +" > <img src=\'" + result.items[i].media.m + "\'> </a></div><p class='author'>" + result.items[i].author.match(/\(([^)]*)\)/)[1] + "</p><p class='published'>" + result.items[i].published + "</p>";
You could use the RegExp matcher "\d" for this but I think a better solution would be to parse the Date with
var d = new Date(result.items[i].published)
From that you get a proper Date object.
you can use a simple regular expression like:
result.items[i].published.replace(/[a-z]/i , "");
or if you want to replace letters with spaces:
result.items[i].published.replace(/[a-z]/i , " ");
the two examples I provided will match ANY letter in your string and replace them, if you want to match more chars than just letters you can simply modify the regular expression.
Hope this answers your question,
Ignazio
Because the JS date object is hell to work with , the best solution is to run a simple REGEX replace to the string.
result.items[i].published.replace(/Z|T/gi," ")
If you need to work and do some advanced stuff with date i recommend to you the dateJS library.
Related
I can't seem to get my head around javascript regex, so I need your help!
I need to transform the following:
1234567891230
Into:
urn:epc:id:sgln:12345678.9123.0
I already did it with a normal javascript algorithm (see underneath), but we need to be able to configure this transformation. I just need it for the default configuration value!
Using slice it would be:
var result = "urn:epc:id:sgln:" + myString.slice(0, 8) + "." +
myString.slice(8, 12) + "." + myString.slice(12);
If you can include an explanation in your answer I would be grateful :)
If you want to use regex for this try the following:
var regex = /(\d{8})(\d{4})/;
var splittedNumber = regex.exec("123456789123");
var result = "urn:epc:id:sgln:"+splittedNumber[1]+"."+splittedNumber[2]+".0";
console.log(result);
But I would recommend the string split you did already.
You could use a regex to capture 3 groups for 12345678, 9123 and 0 and use a word boundary \b at the begin and at the end.
Then using slice you could get all elements but leave out the first element from the array returned by match because that contains the full match that we don't need.
After that you could join the elements from the array using the dot as the separator.
\b(\d{8})(\d{4})(\d)\b
let str = "1234567891230";
let prefix = "urn:epc:id:sgln:";
console.log(prefix + str.match(/\b(\d{8})(\d{4})(\d)\b/).slice(1).join("."));
I'm trying to figure out how to do a RegEx search on a string that ignores case, but when it does a replacement it should preserve the original case.
i.e.
Searching for "adv" should match "Adv", but the replacement should match "Adv" not "adv"
The purpose is to 'highlight' text in a string. Obviously if there is something easier I'm all ears.
Current code...
$("#SearchResults").append(appendString.replace(
new RegExp($("#SearchInput").val(), "g"), "<strong>" +
$("#SearchInput").val() + "</strong>")
);
You need to encapsulate the value with parenthesis, this will create a group and you will be able the get that group in the replacement string with $n where n is the number of the group. The index start to 1.
So use that :
appendString.replace(new RegExp('(' + $("#SearchInput").val() + ')', "gi"), "<strong>$1</strong>");
Note that using regexp like that is dangerous, for instance, if I write (hello in the input, it will throw an error : invalid regexp.
Karl's approach is right, although you might also need to escape special characters:
var value = $("#SearchInput").val();
var escaped = value.replace(/([\[\]\(\)\/\.\*\+])/g, "\\$1");
// Regex: new RegExp(escaped, "gi");
// Replace with: "<strong>" + value + "</strong>");
Edit for typo.
I've never really used regex so this is probably a basic question, but I need to reformat a string in javascript/jquery and I think regex is the direction to go.
How can I convert this string:
\"1\",\"2\",\"\\",\"\4\"
into:
"1","2","","4"
These are both strings, so really they'd be contained in "" but I thought that may confuse things even more.
I've tried the following but it doesn't work:
var value = '\"1\",\"2\",\"\\",\"\4\"'.replace(/\"/, '"').replace(/"\//, '"');
Try:
var value = your_string.replace(/\\/g, "");
to remove all the "\"
It's a lot of escaping... Your string is:
var str = '\\"1\\",\\"2\\",\\"\\\\",\\"\\4\\"'
console.log(str.replace(/\\/g, '')) // "1","2","","4"
However, if you want only to replace \" with " use:
console.log(str.replace(/\\"/g, '"')) // "1","2","\","\4"
I'd like to use Javascript to replace all instances of \u009 in a string
This doesn't seem to be working: .replace(/\u0009/g,'');
Do I need to escape something?
First, the question says "replace all instances of \u009 in a string".
But, the regex has replace(/\u0009/g,''); Is this a typo (different number of zeroes)?
Anyway, if the string only contains, unicode, horizontal tab characters (just one char), then the regex is fine.
If it actually contains 6 ascii chars, then the regex needs to be escaped, like so:
var oneChar = 'Pre \u0009 post';
var sixChars = 'Pre \\u0009 post';
//-- NOTE: If not using Firebug, replace 'console.log()' with 'alert()'.
console.log (oneChar + ' becomes --> ' + oneChar.replace (/\u0009/g, "") );
console.log (sixChars + ' becomes --> ' + sixChars.replace (/\\u0009/g, "") );
You need another escape .replace(/\\u009/g,''); :)
I need a regular expression to strip out any BBCode in a string. I've got the following (and an array with tags):
new RegExp('\\[' + tags[index] + '](.*?)\\[/' + tags[index] + ']');
It picks up [tag]this[/tag] just fine, but fails when using [url=http://google.com]this[/url].
What do I need to change? Thanks a lot.
I came across this thread and found it helpful to get me on the right track, but here's an ultimate one I spent two hours building (it's my first RegEx!) for JavaScript and tested to work very well for crazy nests and even incorrectly nested strings, it just works!:
string = string.replace(/\[\/?(?:b|i|u|url|quote|code|img|color|size)*?.*?\]/img, '');
If string = "[b][color=blue][url=www.google.com]Google[/url][/color][/b]" then the new string will be "Google". Amazing.
Hope someone finds that useful, this was a top match for 'JavaScript RegEx strip BBCode' in Google ;)
You have to allow any character other than ']' after a tag until you find ' ]'.
new RegExp('\\[' + tags[index] + '[^]]*](.*?)\\[/' + tags[index] + ']');
You could simplify this to the following expression.
\[[^]]*]([^[]*)\[\\[^]]*]
The problem with that is, that it will match [WrongTag]stuff[\WrongTag], too. Matching nested tags requires using the expression multiple times.
You can check for balanced tags using a backreference:
new RegExp('\\[(' + tags.Join('|') + ')[^]]*](.*?)\\[/\\1]');
The real problem is that you cant't match arbitrary nested tags in a regular expression (that's the limit of a regular language). Some languages do allow for recursive regular expressions, but those are extensions (that technically make them non-regular, but doesn't change the name that most people use for the objects).
If you don't care about balanced tags, you can just strip out any tag you find:
new RegExp('\\[/?(?:' + tags.Join('|') + ')[^]]*]');
To strip out any BBCode, use something like:
string alltags = tags.Join("|");
RegExp stripbb = new RegExp('\\[/?(' + alltags + ')[^]]*\\]');
Replace globally with the empty string. No extra loop necessary.
I had a similar problem - in PHP not Javascript - I had to strip out BBCode [quote] tags and also the quotes within the tags. Added problem in that there is often arbitrary additional stuff inside the [quote] tag, e.g. [quote:7e3af94210="username"]
This worked for me:
$post = preg_replace('/[\r\n]+/', "\n", $post);
$post = preg_replace('/\[\s*quote.*\][^[]*\[\s*\/quote.*\]/im', '', $post);
$post = trim($post);
lines 1 and 3 are just to tidy up any extra newlines, and any that are left over as a result of the regex.
I think
new RegExp('\\[' + tags[index] + '(=[^\\]]+)?](.*?)\\[/' + tags[index] + ']');
should do it. Instead of group 1 you have to pick group 2 then.
Remember that many (most?) regex flavours by default do not let the DOT meta character match line terminators. Causing a tag like
"[foo]dsdfs
fdsfsd[/foo]"
to fail. Either enable DOTALL by adding "(?s)" to your regex, or replace the DOT meta char in your regex by the character class [\S\s].
this worked for me, for every tag name. it also supports strings like '[url="blablabla"][/url]'
str = str.replace(/\[([a-z]+)(\=[\w\d\.\,\\\/\"\'\#\,\-]*)*( *[a-z0-9]+\=.+)*\](.*?)\[\/\1\]/gi, "$4")