Javascript Regex to match emoticon symbols in my array - javascript

I have this array that has emo symbols and associated image files for each emo path.
Working demo of my code on JSFIDDLE
But using this code, only :) this emo returns the correct smile.png image but rest of the emo are not working.
How do I write the correct regex that match each of these symbols and choose the appropriate file for each emo ?
//Replace Emo with images
function replaceEmoticons(text) {
var emoticons = {
':)' : 'smile.png',
': )' : 'smile.png',
':D' : 'laugh.png',
':->' : 'laugh.png',
':d' : 'laugh-sm.png',
':-)': 'smile-simple.png',
':p': 'tounge.png',
':P': 'tounge-lg.png',
': P': 'tng1.png',
'>-)': 'evil.png',
':(': 'sad.png',
':-(': 'sadd.png',
':-<': 'sadd.png',
':-O': 'surprise.png',
':O': 'sur2.png',
':o': 'sur3.png',
':-o': 'sur3.png',
':-*': 'kiss.png',
':*': 'kiss.png',
':-#': 'angry.png',
':#': 'angry.png',
':$': 'con2.png',
':-$': 'con1.png',
'O.o': 'con2.png',
'o.O': 'con2.png',
':/': 'weird.png',
':x': 'x.png',
':X': 'x.png',
':!': 's.png',
'(:I': 'egg.png',
'^.^': 'kit.png',
'^_^': 'kit.png',
';)': 'wink.png',
';-)': 'wink.png',
":')": 'hc.png',
":'(": 'cry.png',
"|-O": 'yawn.png',
"-_-": 'poke.png',
":|": 'p1.png',
"$_$": 'he.png'
}, url = "images/emo/";
// a simple regex to match the characters used in the emoticons
return text.replace(/[:\-)D]+/g, function (match) {
return typeof emoticons[match] != 'undefined' ?
'<img class="emo" src="'+url+emoticons[match]+'"/>' :
match;
});
}
replaceEmoticons("Hi this is a test string :) with all :P emos working :D");

This regex:
[:\-)D]+
Does not match many of the emoticons in your list. Any character other than :, \, -, ),or D will prevent it from being recognized.
If you have a list of strings you want to match, you can easily build a regex to match any of them (and nothing else) by escaping each one and joining them together with |. Something like this:
// given a string, return the source for a regular expression that will
// match only that exact string, by escaping any regex metacharacters
// contained within.
RegExp.escape = function(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
// build a regex that will match all the emoticons. Written out, it looks
// like this: /:\)|: \)|:D|:->|:d|:-\)|:p|:P|..../g
var emoticonRegex =
new RegExp(Object.keys(emoticons).map(RegExp.escape).join('|'), 'g');
Then use that in place of your literal regex:
return text.replace(emoticonRegex, function (match) { ...

Related

regex for ignoring character if inside () parenthesis?

I was doing some regex, but I get this bug:
I have this string for example "+1/(1/10)+(1/30)+1/50" and I used this regex /\+.[^\+]*/g
and it working fine since it gives me ['+1/(1/10)', '+(1/30)', '+1/50']
BUT the real problem is when the + is inside the parenthesis ()
like this: "+1/(1+10)+(1/30)+1/50"
because it will give ['+1/(1', '+10)', '+(1/30)', '+1/50']
which isn't what I want :(... the thing I want is ['+1/(1+10)', '+(1/30)', '+1/50']
so the regex if it see \(.*\) skip it like it wasn't there...
how to ignore in regex?
my code (js):
const tests = {
correct: "1/(1/10)+(1/30)+1/50",
wrong : "1/(1+10)+(1/30)+1/50"
}
function getAdditionArray(string) {
const REGEX = /\+.[^\+]*/g; // change this to ignore the () even if they have the + sign
const firstChar = string[0];
if (firstChar !== "-") string = "+" + string;
return string.match(REGEX);
}
console.log(
getAdditionArray(test.correct),
getAdditionArray(test.wrong),
)
You can exclude matching parenthesis, and then optionally match (...)
\+[^+()]*(?:\([^()]*\))?
The pattern matches:
\+ Match a +
[^+()]* Match optional chars other than + ( )
(?: Non capture group to match as a whole part
\([^()]*\) Match from (...)
)? Close the non capture group and make it optional
See a regex101 demo.
Another option could be to be more specific about the digits and the + and / and use a character class to list the allowed characters.
\+(?:\d+[+/])?(?:\(\d+[/+]\d+\)|\d+)
See another regex101 demo.

Return full string if partial string is found Javascript/Jquery

Unable to retrieve full string if partially matched.
Example:
src = 'The expression $ a{\color{blue}{x}}^2 + b{\color{blue}{x}} + c$ is said to be quadratic when TAtrimg001a.svg is \neq 0$'
search for "svg" > should return TAtrimg001a.svg
I am trying to search and find the string "svg". If the "svg" exists then it should return TAtrimg001a.svg not just its location or the word svg itself but the complete svg filename.
In reply to a comment...
I tried finding the match in following differenet ways, but they do really work for my requirment, example:
var res = str.match(/svg/ig);
var res = str.search("svg");
var res = str.indexOf( "svg" )
Straightforward with regex. The string .match method takes a regex and returns either:
null if there was no match.
An array otherwise, where the first element is the entire matched string, and the remaining elements are each respective capture group (if any).
So for this case, you just want the whole match, so just taking that first item should be fine. The example regex below just looks for any string of non-whitespace characters that ends with .svg. You may want to broaden or tighten that to meet your exact use case.
src = 'The expression $ a{\color{blue}{x}}^2 + b{\color{blue}{x}} + c$ is said to be quadratic when TAtrimg001a.svg is \neq 0$'
function findFileName(str, ext) {
const match = str.match(new RegExp(`\\w+\\.${ext}`));
return match && match[0]
}
console.log(findFileName(src, "svg"))
Minor Note: When passing a string to the RegExp constructor, backslashes must be doubled, since the first backslash escapes the second as part of the string.
In ES6 you can do something like const result = str.endsWith(".svg") ? str : null;, which will store in result variable full file name (if it ends with ".svg" part, in other words, has svg format), or null (if it doesn't):
function checkIsFileOfType(str, fileType) {
return str.endsWith("." + fileType) ? str : null;
}
console.log(checkIsFileOfType("TAtrimg001a.svD", "svg"));
console.log(checkIsFileOfType("TAtrimg001a.svg", "svg"));

Regex match cookie value and remove hyphens

I'm trying to extract out a group of words from a larger string/cookie that are separated by hyphens. I would like to replace the hyphens with a space and set to a variable. Javascript or jQuery.
As an example, the larger string has a name and value like this within it:
facility=34222%7CConner-Department-Store;
(notice the leading "C")
So first, I need to match()/find facility=34222%7CConner-Department-Store; with regex. Then break it down to "Conner Department Store"
var cookie = document.cookie;
var facilityValue = cookie.match( REGEX ); ??
var test = "store=874635%7Csomethingelse;facility=34222%7CConner-Department-Store;store=874635%7Csomethingelse;";
var test2 = test.replace(/^(.*)facility=([^;]+)(.*)$/, function(matchedString, match1, match2, match3){
return decodeURIComponent(match2);
});
console.log( test2 );
console.log( test2.split('|')[1].replace(/[-]/g, ' ') );
If I understood it correctly, you want to make a phrase by getting all the words between hyphens and disallowing two successive Uppercase letters in a word, so I'd prefer using Regex in that case.
This is a Regex solution, that works dynamically with any cookies in the same format and extract the wanted sentence from it:
var matches = str.match(/([A-Z][a-z]+)-?/g);
console.log(matches.map(function(m) {
return m.replace('-', '');
}).join(" "));
Demo:
var str = "facility=34222%7CConner-Department-Store;";
var matches = str.match(/([A-Z][a-z]+)-?/g);
console.log(matches.map(function(m) {
return m.replace('-', '');
}).join(" "));
Explanation:
Use this Regex (/([A-Z][a-z]+)-?/g to match the words between -.
Replace any - occurence in the matched words.
Then just join these matches array with white space.
Ok,
first, you should decode this string as follows:
var str = "facility=34222%7CConner-Department-Store;"
var decoded = decodeURIComponent(str);
// decoded = "facility=34222|Conner-Department-Store;"
Then you have multiple possibilities to split up this string.
The easiest way is to use substring()
var solution1 = decoded.substring(decoded.indexOf('|') + 1, decoded.length)
// solution1 = "Conner-Department-Store;"
solution1 = solution1.replace('-', ' ');
// solution1 = "Conner Department Store;"
As you can see, substring(arg1, arg2) returns the string, starting at index arg1 and ending at index arg2. See Full Documentation here
If you want to cut the last ; just set decoded.length - 1 as arg2 in the snippet above.
decoded.substring(decoded.indexOf('|') + 1, decoded.length - 1)
//returns "Conner-Department-Store"
or all above in just one line:
decoded.substring(decoded.indexOf('|') + 1, decoded.length - 1).replace('-', ' ')
If you want still to use a regular Expression to retrieve (perhaps more) data out of the string, you could use something similar to this snippet:
var solution2 = "";
var regEx= /([A-Za-z]*)=([0-9]*)\|(\S[^:\/?#\[\]\#\;\,']*)/;
if (regEx.test(decoded)) {
solution2 = decoded.match(regEx);
/* returns
[0:"facility=34222|Conner-Department-Store",
1:"facility",
2:"34222",
3:"Conner-Department-Store",
index:0,
input:"facility=34222|Conner-Department-Store;"
length:4] */
solution2 = solution2[3].replace('-', ' ');
// "Conner Department Store"
}
I have applied some rules for the regex to work, feel free to modify them according your needs.
facility can be any Word built with alphabetical characters lower and uppercase (no other chars) at any length
= needs to be the char =
34222 can be any number but no other characters
| needs to be the char |
Conner-Department-Store can be any characters except one of the following (reserved delimiters): :/?#[]#;,'
Hope this helps :)
edit: to find only the part
facility=34222%7CConner-Department-Store; just modify the regex to
match facility= instead of ([A-z]*)=:
/(facility)=([0-9]*)\|(\S[^:\/?#\[\]\#\;\,']*)/
You can use cookies.js, a mini framework from MDN (Mozilla Developer Network).
Simply include the cookies.js file in your application, and write:
docCookies.getItem("Connor Department Store");

Javascript - How to join two capitalize first letter of word scripts

I have an Acrobat form with some text fields with multiline on. My goal is to convert to uppercase the first letter of any sentence (look for dots) and also the first letter of any new line (after return has been pressed).
I can run each transformation separately, but do not know how to run them together.
To capitalize sentences I use the following code as custom convalidation :
// make an array split at dot
var aInput = event.value.split(". ");
var sCharacter = '';
var sWord='';
// for each element of word array, capitalize the first letter
for(i = 0; i <aInput.length; i++)
{
aInput[i] = aInput[i].substr(0, 1).toUpperCase() + aInput[i].substr(1) .toLowerCase();
}
// rebuild input string with modified words with dots
event.value = aInput.join('. ');
To capitalize new lines I replace ". " with "\r".
Thanks in advance for any help.
You can get the first character of each sentence with RegExp :
event.value = event.value.replace(/.+?[\.\?\!](\s|$)/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
Demo : http://jsfiddle.net/00kzc370/
Regular Expression explained :
/.+?[\.\?\!](\s|$)/g is a regular expression.
.+?[\.\?\!](\s|$) is a pattern (to be used in a search) that match sentences ended by ., ? or ! and followed by a whitespace character.
g is a modifier. (Perform a global match (find all matches rather than stopping after the first match)).
Source : http://www.w3schools.com/jsref/jsref_obj_regexp.asp

Regular expressions - Replacing with XRegExp

I have a feeling this is going to be a stupid question, but I'm completely baffled.
I'm using the XRegExp library for Javascript to generate a regular expression that matches any word in any alphabet, and does not match words with numbers, punctuation or special characters. The library is found here: http://xregexp.com/
This is what I have so far:
var tests = [
"Hello",
"Hello789",
"Hello£*&£",
"你好世界",
"你好世界((£&"
];
var reg = XRegExp('^\\p{L}+$');
for (var i = 0; i < tests.length; i++) {
console.log (tests[i] + " : " + (reg.test(tests[i]) ? "true" : "false"));
}
This produces the following output:
Hello : true
Hello789 : false
Hello£*&£ : false
你好世界 : true
你好世界((£& : false
I know that reg is matching the right thing, but how do I use it in a replace so that the numbers, punctuation and special characters are stripped out? The output I want is :
Hello : Hello
Hello789 : Hello
Hello£*&£ : Hello
你好世界 : 你好世界
你好世界((£& : 你好世界
These are the things I've tried so far, with no success:
XRegExp.replace(tests[i], '^\\p{L}+$', ''));
XRegExp.replace(tests[i], '\\p{L}+$', ''));
XRegExp.replace(tests[i], '^\\p{L}', ''));
XRegExp.replace(tests[i], '\\p{L}', ''));
All of these return the same string I gave them, with no changes at all. Any one have an idea what I'm doing wrong?
Your regular expression is matching only letters, but it sounds like you want to match everything except letters:
var reg = XRegExp('\\P{L}+', 'g');
Then you can replace everything that matches with the empty string.
XRegExp.replace(tests[i], reg, '');

Categories

Resources