I'm trying to create a small program that converts a string to a JavaScript variable. I am trying to figure out how to do this. I can't seem to figure out how to use .match...
Here is my code
function convertVariables(){
var matches = code.match(/#[^;]+/g);
for(var i = 0; i < matches.length; i++){
code = code.replace(matches[i].substring(0, 1), "var ");
console.log("Found one");
}
}
If the input would be:
#somevariable = "some string";
#anothervariable = 12;
The output should be:
var somevariable = "some string";
var anothervariable = 12;
Related
I'm still learning JavaScript but I just can't seem to find a way to see if a string contains a substring.
Basically I have some Titles of some individuals and I want to see if the Title contains strings like "President" or "Sr" in the title this is what i have so far but does not seem to be working.
var title = "President of Sales";
var arrayOfTitles = ["President","Chief","VP","SVP","Director","Manager","Mrg","Sr","Senior","Executive Assistant","Principle Architect","GM","Technical Advisor"];
var re = new RegExp(arrayOfTitles.join("|"),"i");
for(i = 0; i < arrayOfTitles.length; i++){
if ( re.test(gr.title)){
return;
}
}
However this code will not work with String likes "Jr VP" or "President of Sales". Is there a way to build an array of Regex of these strings?
any help would be great thanks
You do not need to run a loop
var title = "President of Sales";
var arrayOfTitles = ["President","Chief","VP","SVP","Director","Manager","Mrg","Sr","Senior","Executive Assistant","Principle Architect","GM","Technical Advisor"];
var regex = new RegExp(arrayOfTitles.join("|"), "i");
//The regex will return true if found in the array
if ( regex.test(title) ){
console.log("has");
}
How about something simple like :
var title = "President of Sales";
var arrayOfTitles = ["President","Chief","VP","SVP","Director","Manager","Mrg","Sr","Senior","Executive Assistant","Principle Architect","GM","Technical Advisor"];
var matches = (function() {
for(var i=0; i < arrayOfTitles.length; i++) {
if(title.indexOf(arrayOfTitles[i]) > -1) { return true; }
}
return false;
}());
You can also use the includes() function, instead of indexOf().
var title = "President of Sales";
var arrayOfTitles = ["President","Chief","VP","SVP","Director","Manager","Mrg","Sr","Senior","Executive Assistant","Principle Architect","GM","Technical Advisor"];
var matches = (function() {
for(var i=0; i < arrayOfTitles.length; i++) {
if(title.includes(arrayOfTitles[i])) { return true; }
}
return false;
}());
Sample string:
(13.910074099911057%2C+100.37796020507812)%2C(13.840746785080066%2C+100.27908325195312)%2C(13.712703652698178%2C+100.33126831054688)%2C(13.7620619168356%2C+100.50979614257812)
Correct format:
13.910074099911057, 100.37796020507812 13.840746785080066, 100.27908325195312 13.712703652698178, 100.33126831054688 13.7620619168356, 100.50979614257812
Sample code:
var locate = window.location
document.GetPerimeter.Perimeter.value = locate
var text = document.GetPerimeter.Perimeter.value
function CopyPerimeter(str) {
theleft = str.indexOf("=") + 1;
theright = str.lastIndexOf("&");
return (str.substring(theleft, theright));
}
var ShowPerimeter = CopyPerimeter(text)
document.GetPerimeter.Perimeter.value = ShowPerimeter
function decode() {
var obj = document.getElementById('Perimeter');
var encoded = obj.value;
obj.value = decodeURIComponent(encoded.replace(/\+/g, ""));
}
decode();
This is the script which parses your JAVASCRIPT String to the desired one:
var str = "(13.910074099911057%2C+100.37796020507812)%2C(13.840746785080066%2C+100.27908325195312)%2C(13.712703652698178%2C+100.33126831054688)%2C(13.7620619168356%2C+100.50979614257812)";
var strParsed = "";
var percentCame = 0;
for (var i=0; i < str.length; i++) {
if( (str[i]!="(") && (str[i]!=")") ){
if( (str[i]=="%") ){
percentCame = 1;
}
if(percentCame==1){
i += 4;
strParsed = strParsed + "," + str[i];
percentCame = 0;
}
else{
strParsed = strParsed + str[i];
}
}
}
You can use Regular Expressions to parse text in Javascript. Just use the myString.match('myRegEx') function and it will return an array with parsed values. Mind that the values are still in text format so they need to be cast into floats if you want to use them as such.
This regular expression might do the trick: /[-+]?\d+\.?\d*[^)C%]/g
If you need to modify the regular expressions to match other values, try using a online helper as the ones below:
http://www.regexr.com/
https://regex101.com/#javascript
I hope I understood your question correct.
I am very, very new at JS with no programming experience and I am struggling with creating a script that counts words in a text box. I have the following code and I can't get anything to populate:
var myTextareaElement = document.getElementById("myWordsToCount");
myTextareaElement.onkeyup = function(){
var wordsCounted = myTextareaElement.value;
var i = 0;
var str = wordsCounted;
var words = str.split('');
for (var i = words.length; i++) {if (words[i].length > 0; i++) { words[i] };
}
And for the Span Id in my HTML, I put the following:
<span id="wordsCounted"></span>
Any direction I where I am royally messing up would be great. I have tried it in JS fiddle and can't get it to populate.
The split method needs a proper character, you can use an space " " or a regex to indicate any whitespace character: "My name is XXX".split(/\s+/) will show ["My", "name", "is", "XXX"].
If you just want the number of words you can do "My name is XXX".split(/\s+/).length, which will return 4.
Try this, this may do what you want. Instead of doing a for loop, just count how many words are there and display the length of the array.
var myTextareaElement = document.getElementById("myWordsToCount");
myTextareaElement.onkeyup = function(){
var wordsCounted = myTextareaElement.value;
var i = 0;
var str = wordsCounted;
var words = str.split('');
if (words.length > 0){
document.getElementById('wordsCounted').innerHTML = words.length;
}
}
Example
var value = "foo bar (foo(bar)(foo(bar)))";
And the value you want is
(foo(bar)(foo(bar)))
And not
(foo(bar)
As elclarns notes, JS doesn't have recursive regex, but regex is not the only tool, parenthesis counter should work, well
var x = "foo bar (foo(bar)(foo(bar))) foo bar";
var result = "";
var cnt = 0;
var record = false;
for(var i=0; i<x.length; ++i) {
var ch = x.charAt(i);
if(ch=="(") { ++cnt; record = true; }
if(ch==")") --cnt;
if(record) result+= ch;
if(record && !cnt) break;
}
if(cnt>0) record = ""; // parenthesis not enclosed
console.log(result); // (foo(bar)(foo(bar)))
This of course captures only the first parenthesis, but you can record them all in array and choose the longest result. This should be easy.
this should work.
var re = /\(.*\)/
var result = re.exec(value) //["(foo(bar)"]
It then will catch the biggest string between parenthesis.
Can anybody tell me how can i get a numeric value from a string containing integer value and characters?
For example,I want to get 45 from
var str="adsd45";
If your string is ugly like "adsdsd45" you can use regex.
var s = 'adsdsd45';
var result = s.match(/([0-9]+)/g);
['45'] // the result, or empty array if not found
You can use regular expression.
var regexp = /\d+/;
var str = "this is string and 989898";
alert (str.match(regexp));
Try this out,
var xText = "asdasd213123asd";
var xArray = xText.split("");
var xResult ="";
for(var i=0;i< xArray.length - 1; i++)
{
if(! isNan(xArray[i])) { xResult += xArray[i]; }
}
alert(+xResult);
var str = "4039";
var num = parseInt(str, 10);
//or:
var num2 = Number(str);
//or: (when string is empty or haven't any digits return 0 instead NaN)
var num3 = ~~str;
var strWithChars = "abc123def";
var num4 = Number(strWithChars.replace(/[^0-9]/,''));