Deformat Discord Text - javascript

I have been trying to make a "deformatting" function that escapes all formatting characters (*,`,_,|,~) and I have actually created this, but I want to check if the user already escaped it to prevent formatting happening on accident.
My current function:
function deformat(string) {
return string.replace(/([*_~|`])/g, '\\$1');
}
I would love to be able to improve it so that a string like "\*hey\*" doesn't become "\\*hey\\*", which would italicize it. (Note that the slashes are not escaped in the previous two string for readability)

Try this:
function deformat(string) {
return string.replace(/(?<!\\)([*_~|`])/g, '\\$1');
}
console.log(deformat('*hey*'));
console.log(deformat('\*hey\*'));
Edit:
Since the above didn't work, try this:
function deformat(string) {
return string.replace(/(^|[^\\])([*_~|`])/g, '$1\\$2');
}
console.log(deformat('*hey*'));
console.log(deformat('\*hey\*'));
It uses (^|[^\\]), which matches the start of the string or anything that isn't '\', instead of a negative lookbehind.

Related

Replace a string fails in some cases

I must check a string and replace something of them.
I have to check if
$mystring='abc...';
has one of this values px,em,%,vh,s
I use this to check the string
function replaceUnit(input){ return input.replace(/(px|em|%|vh|s)/i ,'') }
It works but produces error in some cases.
If I have in my string for example
$mystring="its embeded"
The function will replace the "s" and "em" that's not the way it should be.
The function should check if in mystring is
only a number+px
or only a number+em
or only a number+%
or only a number+vh
or only a number+s
If there is a match, the function should replace the textpart, in all other cases the function should do nothing.
Is it possible to create a kind of this function and how a replace code must be?
Thanks a lot.
UPDATE
based on one of the answears i trie to change it
var input="0s";
function replaceUnit(input)
{
console.log('check: '+input);
var test=input.replace(/(\d)(?:px|em|%|vh|s)$/i ,'');
console.log('->: '+test);
return test
}
the result in the console is
check: 0s
->:
Add a $ (end-of-string anchor) to the end of the regular expression, to ensure that it'll only match if the characters occur at the very end, and capture a number before those characters, so that you can replace with that number alone (thus stripping out the extra characters):
return input.replace(/(\d)(?:px|em|%|vh|s)$/i ,'$1')
https://regex101.com/r/IodB6z/1

Escaping a string to be placed in generated JavaScript source, in JavaScript

I'm writing code that will generate some javascript. The javascript will involve assigning a variable in the generated code to a string passed into the generator. The generator is also in javascript.
Basically I want to do this:
function generate_code(text) {
return "var a = " + jsEscapeString(text) + "; alert(a);";
}
function jsEscapeString(text) {
// WHAT GOES HERE?
// e.g. it needs to:
// - surround with quotes
// - escape quotes inside the text
// - escape backslashes and newlines and other fun characters
// - defend against other horrible things I probably don't know about
}
I don't want something that only works in the happy case. I want something correct. Something that would survive a malicious adversary trying to do sandbox escapes on the resulting code (e.g. like what you do in the game 'Untrusted').
Super easy.
function jsEscapeString(text) {
return JSON.stringify(text);
}
No matter what you put in, you will ALWAYS get a valid representation of it that can be dumped into JS source. The result, when executed, will always be exactly what you put in.
This even works for strings, booleans, numbers, arrays, objects... basically everything you'll ever need.
Although I'm curious as to why you're doing this... This smells of eval fishiness...
You would need to escape backslash, the string delimiter, and control characters:
function jsEscapeString(text) {
return '"' +
text
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')
.replace(/\r/g, '\\r')
.replace(/\n/g, '\\n')
.replace(/\t/g, '\\t')
.replace(/\b/g, '\\b')
.replace(/\v/g, '\\v')
.replace(/\f/g, '\\f')
+ '"';
}

Regex for finding a specific pattern in a string

I want to write a regex to test if a string contains ]][[. For ex:[[t]][[t]].
I am able to find a string which ends with ]] using the pattern:
RegExp(/]]/)
But if i try to use the pattern:
RegExp(/]\]\(?=\[\[)/)
for testing [[t]][[t]], the console displays the following error:
Uncaught Syntax Error: Invalid regular expression: /]\]\(?=\[\[)/: Unmatched ')'
Why you have a syntax error:
You are escaping ( with \(, so you get the Unmatched ')' error in \(?=\[\[).
How to fix it:
The best way to do this depends on exactly what you want.
If you just want to check that the string contains ]][[, don't use a regex:
if (yourString.indexOf(']][[') !== -1) {
// do something
}
If you really want to use a regex, you need to escape [s but not ]s:
if (/]]\[\[/.test(yourString)) {
// do something
}
If you really want to use a regex and not capture the [[:
if (/]](?=\[\[)/.test(yourString)) {
// do something
}
If you want to check for matching [[ and ]] (like [[t]]):
if (/\[\[[^[\]]*]]/.test(yourString)) {
// do something
}
If you want to check for two [[..]] strings back-to-back:
if (/(\[\[[^[\]]*]]){2}/.test(yourString)) {
// do something
}
If you want to check for one [[..]] string, followed by exactly the same string ([[t]][[t]] but not [[foo]][[bar]]):
if (/(\[\[[^[\]]*]])\1/.test(yourString)) {
// do something
}
Here's a demo of all of the above, along with a bunch of unit tests to demonstrate how each of these works.
Use this:
if (/]]\[\[/.test(yourString)) {
// It matches!
} else {
// Nah, no match...
}
Note that we need to escape the two opening braces with \[, but the closing braces ] do not need escaping as there is no potential confusion with a character class.
You need to escape the opening square brackets because it has a special meaning in regex which represents the start of a character class.
]](?=\[\[)
Try this one, this regex must match everything you need:
(\w*[\[\]]+\w*)+
But if for every open bracket [ there must be a closed bracket ], that's different.
What exactly you want?

jQuery input filter for textarea

I adapted this solution into my script. The idea is to prevent the user from typing unauthorized characters (of course there is also a filter on the back end).
$('#someinput').keyup(function() {
var $th = $(this);
$th.val( $th.val().replace(/[^a-zA-Z0-9]/g, function(str) {
console.log(str);
return '';
}))
})
It works nice, but I also need the users to be able to type specific allowed characters like: .,!?ñáéíóú - I mean, the basic a-zA-Z0-9 plus some basic chars and the whole bunch of special language characters.
What actually needs to be left out are: ##$%^&*()=_+"':;/<>\|{}[]
Any ideas? Thanks!
Solution thanks to Michael
//query
$('#someinput').keyup(function() {
var $th = $(this);
$th.val($th.val().replace(/[##$%\^&*()=_+"':;\/<>\\\|{}\[\]]/g,function(str){return '';}));
}).bind('paste',function(e) {
setTimeout(function() {
$('#someinput').val($('#someinput').val().replace(/[##$%\^&*()=_+"':;\/<>\\\|{}\[\]]/g,function(str){return '';}));
$('#someinput').val($('#someinput').val().replace(/\s+/g,' '));
},100);
});
Invert your regular expression to only replace the specific characters you want omitted:
$th.val( $th.val().replace(/\s?[##$%\^&*()=_+"':;\/<>\\\|{}\[\]]/g, ""));
// Edit: added optional \s to replace spaces after special chars
Note, a few of them need to be escaped with a backslash inside a [] character class: \\\[\]\^\/
If I'm understanding what you are wanting to do, can't you just add those unwanted characters to your regex instead of doing the [^a-zA-Z0-9]?
Replace that with [##\$%\^&\*\(\)=_\+"':;\/<>\\\|\{\}\[\]] (notice the escaping)

Regular expression for allowing only a certain set of characters

I would like some help creating a regular expression for parsing a string on a textbox. I currently have these two javascript methods:
function removeIllegalCharacters(word) {
return word.replace(/[^a-zA-Z 0-9,.]/g, '');
}
$("#comment").keyup(function() {
this.value = removeIllegalCharacters(this.value);
});
I would like to replace my /[^a-zA-Z 0-9,.]/g regex for one that would accept only the following set of characters:
a-z
A-Z
0-9
áéíóúü
ÁÉÍÓÚÜ
ñÑ
;,.
()
- +
It's probably pretty simple, but I have close to none regex skills. Thanks in advance.
Just add those characters in.
function removeIllegalCharacters(word) {
return word.replace(/[^a-zA-Z 0-9,.áéíóúüÁÉÍÓÚÜñÑ();+-]/g, '');
}
return word.replace(/[^a-zA-Z0-9áéíóúüÁÉÍÓÚÜñÑ\(\);,\.]/g, '');
You may have to use the hex escape sequence (\x##) or unicode escape sequence (\u####) for some of the non standard letters, but that will give you a good start. Or, slightly simplified:
return word.replace(/[^\w\dáéíóúüÁÉÍÓÚÜñÑ\(\);,\.]/g, '');
If I've understood your requirements correctly, you want to allow only the listed char and you want to delete rest all char. If that is the case you can simply extend your char class as:
function removeIllegalCharacters(word) {
return word.replace(/[^a-zA-Z0-9áéíóúüÁÉÍÓÚÜñÑ;,.()]/g, '');
}
Did you try with: [^a-zA-Z 0-9;,.áéíóúüÁÉÍÓÚÜñÑ()]

Categories

Resources