I need to read a string while ignoring any spaces or capital letters - node.js

I'm trying to read any message sent on a discord server and send a reply if a certain string is within the message ignoring all spaces and capitals. I'm very new to javascript and this is the first code I'm making just for fun.
This is the current main part of the code.
if(msg.content.toLowerCase().includes('string'))
{
msg.channel.send(emoji("480351478930866179"));
}

You can remove whitespace with replace() and shift the string to lowercase using toLowerCase() to achieve the desired result.
const original = 'Hello there.';
const str = original.replace(/\s/g, '').toLowerCase();
if (str.includes('hello')) console.log('Hi.');

You could use the string.replace method or you could use split then join. To ignore case just use toLowerCase();

Thank's, that solved my problem.
const original = 'Hello there.';
const str = original.replace(/\s/g, '').toLowerCase();
if (str.includes('hello')) console.log('Hi.');

Related

Unescape encoded string in Node (\x##)

I have the following encoded string in Node;
const test = '\x50\x77\x6b\x6d\x77\x37\x54\x43\x6f\x51\x3d\x3d'
I want to get it's unencoded value Pwkmw7TCoQ==
How can I achieve this?
Use the .toString() method. It should work.
test.toString()
Nothing to do there. Just print the string to the console as it is.
const test = '\x50\x77\x6b\x6d\x77\x37\x54\x43\x6f\x51\x3d\x3d';
console.log(test);
'\x50\x77\x6b\x6d\x77\x37\x54\x43\x6f\x51\x3d\x3d' and 'Pwkmw7TCoQ==' are different notations for the same value.

Why postgres is returning additional backslash in a simple query

So in my node code postgres query is returning double quotes when it's returning its values.
As opposed to the query at pgAdmin.
I already tried to solve it using regex but this attempt was innefective. So if anyone had a problem like this and could help me, I would be glad.
Thanks in advance
There are neither quotes nor extra back slashes in the string. They are part of the string representation as literal.
Try console.log(value) - or even directly console.log('/\\w/g') - and you'll see the output is /\w/g as expected.
To answer my own question, after a lot of reading and researching, I managed to discover that because a backslash character is a special character it will create some problems around its implementation in regex, because it is not permitted to have a lone backslash stored in a variable for example.
This would never work stored inside a variable because the backslash have to be escaped.
/\w+/ig
Javascript will transform it automatically to be able to perform.
/\w+/ig
When reading
RegExp - Javascript documentation, I came across an interesting statement, the RegExp function will recognize and use a double slash regex, thankfully!
So I just adapted my regex to split it's statement from it's flags and mount it again using RegExp.
Below is the code that I used to solve this problem
// Getting values from postgres
const values = (await pgConn.admRead.query(clientQuery)).rows[0].value || [];
// Splitting regex ( values: /\w/g )
const valuesSplit = values.split('/'); // RESULT -> ['', w, g]
// Removing first array item when it's empty
if (valuesSplit[0].length === 0) {
valuesSplit.shift();
}
// Creating regex from splitted array
const regexOperation = new RegExp(valuesSplit[0], valuesSplit[1]);
// Executing replace function
const messageMasked = message.replace(regexOperation, '*');
return messageMasked;

How to remove string after and before specific chars

I have this string: https://2352353252142dsbxcs35#github.com/happy.git
I want to get result: https://github.com/happy.git (without random string after second / and after # but without #).
Now I have something like this:
var s = 'https://2352353252142dsbxcs35#github.com/happy.git';
var d = s.substring(s.indexOf('/')+2, s.indexOf('#')+1;
s = s.replace(d, "");
it works, but I know it's an ugly solution.
What is the most efficient and more universal solution?
Try this:
const indexOfAtSign: number = receivedMessage.indexOf('#')+1
const httpsString: string = 'https://'
const trimmedString: string = s.slice(indexOfAtSign)
const requiredURL: string = httpsString.concat(trimmedString)
// Print this value of requiredURL wherever you want.
So here what my code does is, it gets position of # and removes everything before it along with the sign itself. Then using the slice() function, we are left with the remaining part which I named as trimmedString. Now I have pre-defined the `https string, anf we just need to merge them now. Done :-)
I had tried this out in my telegram bot and here's how it works:

golang remove characters (used for readability) in const string at compile time (spaces, \n and \t)

Spaces are useful to indent urls, sql queries to make it more readable.
Is there a way to remove characters from a const string at compile time in golang ?
ex: (runtime version)
const url = `https://example.com/path?
attr1=test
&attr2=test
`
// this is the code to be replaced
urlTrim := strings.Replace(
strings.Replace(url, "\n", "", -1)
)
Constant expressions cannot contain function calls (except a few built-in functions). So what you want cannot be done using a raw string literal.
If your goal with using multiple lines is just for readability, simply use multiple literals and concatenate them:
const url = "https://example.com/path?" +
"attr1=test" +
"&attr2=test"
Try it on the Go Playground.
See related question: Initialize const variable

Is there any way to retrieve a appended int value to a String in javaScript?

I am currently working on a project that dynamically displays DB content into table.
To edit the table contents i am want to use the dynamically created "string"+id value.
Is there any way to retrieve the appended int value from the whole string in javaScript?
Any suggestions would be appreciative...
Thanks!!!
If you know that the string part is only going to consist of letters or non-numeric characters, you could use a regular expression:
var str = "something123"
var id = str.replace(/^[^\d]+/i, "");
If it can consist of numbers as well, then things get complicated unless you can ensure that string always ends with a non-numeric character. In which case, you can do something like this:
var str = "something123"
var id = str.match(/\d+$/) ? str.match(/\d+$/)[0] : "";
(''+string.match(/\d+/) || '')
Explanation: match all digits in the variable string, and make a string from it (''+).
If there is no match, it would return null, but thanks to || '', it will always be a string.
You might try using the regex:
/\d+$/
to retrieve the appended number

Resources