decimal emoji with using regex - javascript

I want to change my keyboard character with sad emoji using html decimal code and javascript regex. I just want to conflate that two characters ':' and '('
How can I write that emojiSmile regex variable?
Here are my code below.
var finalAnswer = ':)';
var emojiSmile = /\:+[)]/g;
var emojiSmileDecimal = '😊';
if (finalAnswer.match(emojiSmile)) {
var emojies = finalAnswer.replace(emojiSmile, emojiSmileDecimal);
}

It already works as you wrote it.
From first principles, you just need to escape the ) as \) because it's a special character in regex syntax:
var emojiSmile = /:\)/g;
Escaping it with brackets ([)]), like you did, also works:
var emojiSmile = /:[)]/g;
There is no need to escape : or to put a + behind it (unless you want to match ::::) as well).

Related

Javascipt regex to get string between two characters except escaped without lookbehind

I am looking for a specific javascript regex without the new lookahead/lookbehind features of Javascript 2018 that allows me to select text between two asterisk signs but ignores escaped characters.
In the following example only the text "test" and the included escaped characters are supposed to be selected according the rules above:
\*jdjdjdfdf*test*dfsdf\*adfasdasdasd*test**test\**sd* (Selected: "test", "test", "test\*")
During my research I found this solution Regex, everything between two characters except escaped characters /(?<!\\)(%.*?(?<!\\)%)/ but it uses negative lookbehinds which is supported in javascript 2018 but I need to support IE11 as well, so this solution doesn't work for me.
Then i found another approach which is almost getting there for me here: Javascript: negative lookbehind equivalent?. I altered the answer of Kamil Szot to fit my needs: ((?!([\\])).|^)(\*.*?((?!([\\])).|^)\*) Unfortuantely it doesn't work when two asterisks ** are in a row.
I have already invested a lot of hours and can't seem to get it right, any help is appreciated!
An example with what i have so far is here: https://www.regexpal.com/?fam=117350
I need to use the regexp in a string.replace call (str.replace(regexp|substr, newSubStr|function); so that I can wrap the found strings with a span element of a specific class.
You can use this regular expression:
(?:\\.|[^*])*\*((?:\\.|[^*])*)\*
Your code should then only take the (only) capture group of each match.
Like this:
var str = "\\*jdjdjdfdf*test*dfsdf\\*adfasdasdasd*test**test\\**sd*";
var regex = /(?:\\.|[^*])*\*((?:\\.|[^*])*)\*/g
var match;
while (match = regex.exec(str)) {
console.log(match[1]);
}
If you need to replace the matches, for instance to wrap the matches in a span tag while also dropping the asterisks, then use two capture groups:
var str = "\\*jdjdjdfdf*test*dfsdf\\*adfasdasdasd*test**test\\**sd*";
var regex = /((?:\\.|[^*])*)\*((?:\\.|[^*])*)\*/g
var result = str.replace(regex, "$1<span>$2</span>");
console.log(result);
One thing to be careful with: when you use string literals in JavaScript tests, escape the backslash (with another backslash). If you don't do that, the string actually will not have a backslash! To really get the backslash in the in-memory string, you need to escape the backslash.
const testStr = `\\*jdjdjdfdf*test*dfsdf\\*adfasdasdasd*test**test\\**sd*`;
const m = testStr.match(/\*(\\.)*t(\\.)*e(\\.)*s(\\.)*t(\\.)*\*/g).map(m => m.substr(1, m.length-2));
console.log(m);
More generic code:
const prepareRegExp = (word, delimiter = '\\*') => {
const escaped = '(\\\\.)*';
return new RegExp([
delimiter,
escaped,
[...word].join(escaped),
escaped,
delimiter
].join``, 'g');
};
const testStr = `\\*jdjdjdfdf*test*dfsdf\\*adfasdasdasd*test**test\\**sd*`;
const m = testStr
.match(prepareRegExp('test'))
.map(m => m.substr(1, m.length-2));
console.log(m);
https://instacode.dev/#Y29uc3QgcHJlcGFyZVJlZ0V4cCA9ICh3b3JkLCBkZWxpbWl0ZXIgPSAnXFwqJykgPT4gewogIGNvbnN0IGVzY2FwZWQgPSAnKFxcXFwuKSonOwogIHJldHVybiBuZXcgUmVnRXhwKFsKICAgIGRlbGltaXRlciwKICAgIGVzY2FwZWQsCiAgICBbLi4ud29yZF0uam9pbihlc2NhcGVkKSwKICAgIGVzY2FwZWQsCiAgICBkZWxpbWl0ZXIKICBdLmpvaW5gYCwgJ2cnKTsKfTsKCmNvbnN0IHRlc3RTdHIgPSBgXFwqamRqZGpkZmRmKnRlc3QqZGZzZGZcXCphZGZhc2Rhc2Rhc2QqdGVzdCoqdGVzdFxcKipzZCpgOwpjb25zdCBtID0gdGVzdFN0cgoJLm1hdGNoKHByZXBhcmVSZWdFeHAoJ3Rlc3QnKSkKCS5tYXAobSA9PiBtLnN1YnN0cigxLCBtLmxlbmd0aC0yKSk7Cgpjb25zb2xlLmxvZyhtKTs=

regular expression to extract two items from a long string

There are some strings having the following type of format,
{abc=1234457, cde=3, label=3352-4e9a-9022-1067ca63} <chve> abc? 123.456.789, http=appl.com
I would like to extract 1234457 and 3352-4e9a-9022-1067ca63, which correspond to abc and label respectively.
This is the javascript I have been trying to use, but it does not work. I think the regular expression part is wrong.
var headerPattern = new RegExp("\{abc=([\d]*),,label=(.*)(.*)");
if (headerPattern.test(row)) {
abc = headerPattern.exec(row)[0];
label = headerPattern.exec(row)[1];
}
Try: abc=(\d*).*?label=([^}]*)
Explanation
abc= literal match
(\d*) catch some numbers
.*? Lazy match
label= literal match
([^}]*) catch all the things that aren't the closing brace
Here is what I came up with:
\{abc=(\d+).*label=(.+)\}.*
Your have two problems in \{abc=([\d]*),,label=(.*)(.*):
Using abc=([\d]*),,, you are looking for abc=([\d]*) followed by the literal ,,. You should use .* instead. Since .* is nongreedy be default, it will not match past the label.
By using label=(.*)(.*), the first .* captures all the remaining text. You want to only catch text until the edge of the braces, so use (.*)}.*.
Disclaimer: Made with a Java-based regex tester. If anything in JavaScript regexes would invalidate this, feel free to comment.
You can do it the following way:
var row = '{abc=1234457, cde=3, label=3352-4e9a-9022-1067ca63} <chve> abc? 123.456.789, http=appl.com';
var headerPatternResult = /{abc=([0-9]+),.*?label=([a-z0-9\-]+)}/.exec(row);
if (headerPatternResult !== null) {
var abc = headerPatternResult[1];
var label = headerPatternResult[2];
console.log('abc: ' + abc);
console.log('label: ' + label);
}

Replace a period with HTML character

I am having a problem with some code. I put in an input and text in a paragraph is highlighted using <mark>. But when I add a period to highlight all the periods, the code freaks out and gives me the actual html code and has random highlights. So I tried to add a replacer to change the periods. Now it won't freak out but with won't highlight anything. Here is my code to try to replace the period with the html character number (.):
var i = document.getElementById("Bar").value;
var inpu = $.trim(i);
var inp = inpu.replace(".", ".");
var SearchReq = new RegExp("(\\b" + inp + "\\b)", "gim");
var Notes = document.getElementById("NoteHolder").innerHTML;
var after = Notes.replace(SearchReq, "<mark class=" + ColorOptionReady + ">$1</mark>");
document.getElementById("NoteHolder").innerHTML = after;
What is the problem with the code? (I tried removing the "\b" in the regex but that didn't fix it.)
Replace . to \\. ( escape it, \\ backslash required to keep backslash when you would pass string to RegExp) :
var inp = inpu.replace(".", "\\.");
Reason of your error is that . is character which has special meaning in RegExp, so you have to escape it prior passing to RefExp.
See Special characters meaning in regular expressions for more information.
Good Luck )!

Replace all character matches that are not escaped with backslash

I am using regex to replace ( in other regexes (or regexs?) with (?: to turn them into non-matching groups. My expression assumes that no (?X structures are used and looks like this:
(
[^\\] - Not backslash character
|^ - Or string beginning
)
(?:
[\(] - a bracket
)
Unfortunatelly this doesn't work in case that there are two matches next to each other, like in this case: how((\s+can|\s+do)(\s+i)?)?
With lookbehinds, the solution is easy:
/(?<=[^\\]|^)[\(]/g
But javascript doesn't support lookbehinds, so what can I do? My searches didn't bring any easy universal lookbehind alternative.
Use lookbehind through reversal:
function revStr(str) {
return str.split('').reverse().join('');
}
var rx = /[(](?=[^\\]|$)/g;
var subst = ":?(";
var data = "how((\\s+can|\\s+do)(\\s+i)?)?";
var res = revStr(revStr(data).replace(rx, subst));
document.getElementById("res").value = res;
<input id="res" />
Note that the regex pattern is also reversed so that we could use a look-ahead instead of a look-behind, and the substitution string is reversed, too. It becomes too tricky with longer regexps, but in this case, it is still not that unreadable.
One option is to do a two-pass replacement, with a token (I like unicode for this, as it's unlikely to appear elsewhere):
var s = 'how((\\s+can|\\s+do)(\\s+i)?)?';
var token = "\u1234";
// Look for the character preceding the ( you want
// to replace. We'll add the token after it.
var patt1 = /([^\\])(?=\()/g;
// The second pattern looks for the token and the (.
// We'll replace both with the desired string.
var patt2 = new RegExp(token + '\\(', 'g');
s = s.replace(patt1, "$1" + token).replace(patt2, "(?:");
console.log(s);
https://jsfiddle.net/48e75wqz/1/
(EDITED)
string example:
how((\s+can|\s+do)(\s+i)?)?
one line solution:
o='how((\\s+can|\\s+do)(\\s+i)?)?';
alert(o.replace(/\\\(/g,9e9).replace(/\(/g,'(?:').replace(/90{9}/g,'\\('))
result:
how(?:(?:\s+can|\s+do)(?:\s+i)?)?
and of course it works with strings like how((\s+\(can\)|\s+do)(\s+i)?)?

Regex that only works on browser tester

I've tested my regex on Regex testers and it worked, but I didn't get it to work on my code.
var mail = "chdelfosse#gmail.com";
var regExp = new RegExp("#(.*?)\.");
document.write(regExp.exec(mail)) ;
I get this result :
#g,
I tried to add a backslash before the dot, and I got this :
#gmail.,gmail
I also wanted to remove the "#" and the "." from the email, so I tried to use " (?:#) ", but I didn't get it to work (on Regex testers).
It's my first time trying to use Regex, and I don't get it.
Why is there a comma ?
You can use this regex to get the domain name:
/#(.+)\./
Live DEMO
Faster than regex:
var emailAddress = "my.email#gmail.com";
var array_email = emailAddress.split("#");​​
alert('Account: ' + array_email[0] +'; Domain: ' + array_email[1]);​​​​​​​​​​​​​​​​​​​​​​​​​​
A couple things to do differently:
You need to double escape your backslash in the string so that one backslash still remains for the RegExp constructor or switch to the /regex here/ syntax.
If you want just the subgroup in the parens, you need to refer to that specific subgroup.
Here's the code:
var mail = "chdelfosse#gmail.com";
console.log(mail.match(/#(.*?)\./)[1]);

Categories

Resources