How can I check if variable contains Chinese/Japanese characters? - javascript

How do I check if a variable contains Chinese or Japanese characters? I know that this line works:
if (document.body.innerText.match(/[\u3400-\u9FBF]/))
I need to do the same thing not for the document but for a single variable.

.match is a string method. You can apply it to anything that contains string. And, of course, to arbitrary variable.
In case you have something that is not string, most objects define .toString() method that converts its content to some reasonable stringified form. When you retrieve selection from page, you get selection object. Convert it to string and then use match on it: sel.toString().match(...).

afaik you can to the same with a variable... document.body.innerText just returns the text of the body. Therefore you can just do
myvar.match(...)
Here's an example: http://snipplr.com/view/15357/

Related

Capture value surounded by symbol in string

Inside my strings there is values surounded by $:
"$FORMAT$ is invalid, please check $FILE$ file"
I want to take variable names between $ (for example $FORMAT$ should be - FORMAT) to compare and if they match some condition replace variable in string with some value (for example ".txt"). I tried to achive that functionality with regex:
/\$(\w+)\$/gi
But it takes $ with variable names. How can I modify my regex to make it work properly? Thanks!
/\$(.+?)\$/gi should work well in your case.

Remove value from name-value pair string in JavaScript

I have a JavaScript string like dog=1,cat=2,horse=3. The names and values can be anything. I want to remove dog and whatever value is associated with it from the string. So in this example I would end up with cat=2,horse=3. There may not be a entry for dog in the string, and it could be anywhere within the string, e.g. cat=22,dog=17,horse=3 which would end up as cat=22,horse=3.
The names and values will just be alphanumeric with no special characters like quotes and equals signs within them.
What is the best way of going about this in JavaScript?
Simplest solution:
str.split(",").filter(function(kv) {
return kv.slice(0, 4) != "dog=";
}.join(",")
You can do some regex magic as well, but that's not going to be as clear (and maintainable):
str.replace(.replace(/(,|^)dog=[^,]*/g, "").replace(/^,/,"")
You could do this, although may not be the best way:
convert the string to array as it is comma seperated.
remove the dog from the array.
join the array back as a string.

Javascript Function is treating my string argument like an integer

I have a javascript function that contains an argument.
Click here
I need that argument to be a string because sometimes there will be Letters or a Leading 0 that I will need to preserve like in this example.
I checked to make sure that the argument that is passed in is a string with
x.constructor === String
But in my changeAvatar function, the argument comes through as no longer a string. Instead it is a number and removes the leading zero or breaks if it contains a letter.
How do I get the argument to retain it's type?
You've typed it as a javascript number as this line of javascript evaluates it to a number:
changeAvatar(0684839741);
If you want it to be a string, you have to do it this way where you explicitly declare it as a string like:
changeAvatar('0684839741');
which means the whole line should be this:
Click here
Add single quotes:
Click here

How can I split text on commas not within double quotes, while keeping the quotes?

So I'm trying to split a string in javacript, something that looks like this:
"foo","super,foo"
Now, if I use .split(",") it will turn the string into an array containing [0]"foo" [1]"super [2]foo"
however, I only want to split a comma that is between quotes, and if I use .split('","'), it will turn into [0]"foo [1]super,foo"
Is there a way I can split an element expressing delimiters, but have it keep certain delimiters WITHOUT having to write code to concatenate a value back onto the string?
EDIT:
I'm looking to get [0]"foo",[1]"super,foo" as my result. Essentially, the way I need to edit certain data, I need what is in [0] to never get changed, but the contents of [1] will get changed depending on what it contains. It will get concatenated back to look like "foo", "I WAS CHANGED" or it will indeed stay the same if the contents of [1] where not something that required a change
Try this:
'"foo","super,foo"'.replace('","', '"",""').split('","');
For the sake of discussion and benefit of everyone finding this page is search of help, here is a more flexible solution:
var text = '"foo","super,foo"';
console.log(text.match(/"[^"]+"/g));
outputs
['"foo"', '"super,foo"']
This works by passing the 'g' (global) flag to the RegExp instance causing match to return an array of all occurrences of /"[^"]"/ which catches "foo,bar", "foo", and even "['foo', 'bar']" ("["foo", "bar"]" returns [""["", "", "", ""]""].)

Retrieve only a portion of a matched string using regex in Javascript

I've got a string like
foo (123) bar
I want to retrieve all numbers surrounded with the delimiters ( and ).
If I use varname.match(/\([0-9]+\)/), my delimiters are included in the response, and I get "(123)" when what I really want is "123".
How can I retrieve only a portion of the matched string without following it up with varname.replace()?
Yes, use capturing (non-escaped) parens:
varname.match(/\(([0-9]+)\)/)[1]

Categories

Resources