Currently, I am having a problem with a string that I received after sending the command line containing the <0x00> character so how can I remove it from the string?
For example, my var string is:
<0x00>
awplus #
output:
awplus #
Use the following code to get your desired output
var string = "<0x00> awplus #";
string = string.replace("<0x00>", '');
string = string.trim();
document.write (string);
You can simply use split function and pass the string value you want to remove and then select the required part. Please note that split function returns array of string after spliting the original. You must choose the required value from the array.
I've used trim() to remove spaces around the resulting value.
Execute the following code in browser's developer console and you'll get the output.
var str = "<0x00> awplus #";
console.log(str.split('<0x00>')[1].trim());
Related
I need to check whether a string contains other than the specified words/sentence (javascript), it will return true if:
it contains an alphabets, except this phrase: ANOTHER CMD
it contains other than specified multiple sequence of numbers for example: ["8809 8805", "8806 8807"] (the numbers are examples I should be able to test the string for any array of numbers)
Thank you!
Yes you can replace all not in the array
const arr = ["ANOTHER CMD","8809 8805", "8809 8805"]
const okContent = str => {
arr.forEach(entry => str = str.replaceAll(entry,""))
return str.trim()==="";
};
console.log(okContent('Has other stuff than ANOTHER CMD and 8809 8805'))
console.log(okContent('8809 8805 ANOTHER CMD 8809 8805'))
I don't know if it's the correct way of doing it but this worked for me:
replace all the valid words with balnk (using replace)
check if the string is left empty
if it's empty, it means that the string does not contain any unwanted string (to check for space you could use trim method)
you can try regex!
use your array of strings as the '|' separated regex value
and check the specified string in the given line. if it presents negate the output.
const regex = /(ANOTHER CMD|8809 8805|8806 8807)/i
console.log(!regex.test('Should not contain word ANOTHER CMD'))
console.log(regex.test('Should contain word ANOTHER CMD'))
I am pulling in a string from another web page. I want to read that string into a variable but only after a certain point. Eg:
#stringexample
var variable;
I want variable to equal stringexample but not contain the # how could I do this?
This is how I am using the variable at the moment.
$("#Outputajax").load("folder/"+ variable +".html");
This is the way that works but isn't a variable.
$("#Outputajax").load("folder/webpage.html");
If you just want to trim of the first character, then you can use substring...
var input = "#stringexample";
input = input.substring(1);
//input = "stringexample"
Here is a working example
var myVariable = stringExample.replace('#','');
Could just use variable.substr(1) to cut off the first character.
If you want to specifically remove the hash from the start (but do nothing if the hash isn't there), try variable.replace(/^#/,"")
I understand you want to get everything in the string AFTER the hashtag. The other solutions will leave anything ahead of the hashtag in as well. And substring does not work if the hashtag is not the first symbol.
variable= "#stringexample".split("#")[1];
This splits the string into an array of strings, with the parameter as the point where to split, without including the parameter itself. There will be an empty string as the first parameter, and everything after the hashtag is the second string.
var slicer = function(somestring){
var parsedString = somestring;
parsedString = parsedString.slice(1);
return parsedString
}
// run from yors function with some string
var someYouVar = slicer("#something")
I have an data.url string. I want to get some data from it with the following regex
var filename = data.url.match(/file=(.+)&/gi);
All I want is the data inside the parenthesis -a file name actually- but what I get back is "file=example.jpg&".
Why is this happening? Shouldn't only the the matches found in the parentheses be returned?
How can I get rid of those unnecessary characters? Thanks
Javascript returns both the whole matched pattern(usually known as group-0) along with the other matched groups. You can use this one:
var filename = /file=(.+)&/gi.exec(data.url).slice(1);
Use
var filename = data.url.match(/file=([^&]+)/i)[1];
Example:
"file=example.jpg".match(/file=([^&]+)/i)[1] == "example.jpg"
"file=example.jpg&b=ttt&c=42".match(/file=([^&]+)/i)[1] == "example.jpg"
"http://example.com/index.php?file=example.jpg&b=ttt&c=42".match(/file=([^&]+)/i)[1] == "example.jpg"
match() returns an array with the first searched group at the second place, i.e. at match(...)[1].
Note: The result of the above code will be a String. If you still want to have a singleton array with your filename as the only element, then you should take the solution of #Sabuj Hassan.
I've been looking for an answer to this, but whatever method I use it just doesn't seem to cut off the new line character at the end of my string.
Here is my code, I've attempted to use str.replace() to get rid of the new line characters as it seems to be the standard answer for this problem:
process.stdin.on("data", function(data) {
var str;
str = data.toString();
str.replace(/\r?\n|\r/g, " ");
return console.log("user typed: " + str + str + str);
});
I've repeated the str object three times in console output to test it. Here is my result:
hi
user typed: hi
hi
hi
As you can see, there are still new line characters being read between each str. I've tried a few other parameters in str.replace() but nothing seems to work in getting rid of the new line characters.
You are calling string.replace without assigning the output anywhere. The function does not modify the original string - it creates a new one - but you are not storing the returned value.
Try this:
...
str = str.replace(/\r?\n|\r/g, " ");
...
However, if you actually want to remove all whitespace from around the input (not just newline characters at the end), you should use trim:
...
str = str.trim();
...
It will likely be more efficient since it is already implemented in the Node.js binary.
You were trying to console output the value of str without updating it.
You should have done this
str = str.replace(/\r?\n|\r/g, " ");
before console output.
you need to convert the data into JSON format.
JSON.parse(data) you will remove all new line character and leave the data in JSON format.
I have values seperated by pipes in a database. But the issue is that I am appending | at every entry.
For Example:
|275634|374645|24354|
How can I remove the first pipe from the whole string not all the pipes.
Once inserted I don't need to check for the next time when it updates.
If I use substring(1) then it will remove the first character every time,
Please suggest a fix?
//input = '|275634|374645|24354|';
output = input.replace('|', '');
String#replace will replace the first occurance in a String. If you replace it with an empty String '' it is removed.
jsFiddle
you can use substring(index) method where ever you want to remove the perticular special character from the String: like
String str2 = "|275634|374645|24354|";
str2 = str2.substring(1);
System.out.println(str2);
you can see the output as 275634|374645|24354|