Java Script and RegEx - javascript

In application that I'am working on I have dynamically generated UI it is number of controls and listed entries depends on records from DB.What I'am trying to achieve is to write RegEx that would help me extract some numbers assigned as part of controls ID.
The way how I'am assigning IDs is : cText+(row that it represents in its group)+G+(group number).For example cText4G12
What I'am looking for is to get from that ID what Row it represents in example is 4 and it's group 12. I've never used RegEx before so I'am asking for your help :)

RegEx is not something that is easy to get your head around, so I would strongly recommend that you spend a bit of time to learn it - as it will be beneficial to you in the long run.
However, against my best judgement - here is code that will get you what you need..
var id = "cText4G12";
var result = id.match(/^cText(\d+)G(\d+)$/);
result[1] will contain "4"
result[2] will contain "12"

You don't really need regex. You can simply do:
var my_id = "cText4G12";
var row = my_id.slice(my_id.indexOf("cText")+5,my_id.indexOf("G"));
var group = my_id.slice(my_id.indexOf("G")+1);

Related

how can i create a reg exp or array to change this?

ok guys i have been working on these, this is like my main project and im pretty anxious about it, i have been practicing but im still a newbie. My code is like this and it does what it has to do, but im thinking that it could be improved to be better and more reusable, sorry to spam if i am, i already asked on the spanish version of the website with no satisfactory answer, im new to web developing and to this site, i always read the content on this site to answer my questions but for this time i didnt know how to exactly use the previous answers to fix my code, since im new to web developing and im trying to use jquery bit by bit. As i said my question is how can i create an array or a reg exp that does all the things this code does? without having to use .replace function all those times
i have tried urlencode function, and tried to iterate over arrays on jquery but i still dont know how to do it properly.
$( ".linkbestia" ).each(function() {
lnk = $(this).text();
enlace= $(this).attr("href");
espacios=lnk.replace(" ","_");
maslimpio=espacios.replace("'","%27");
muchomaslimpio=maslimpio.replace("(","%28");
muchomuchomaslimpio=maslimpio.replace(")","%29");
nuevoenlace=$(this).attr("href",enlace+muchomuchomaslimpio);
});
the actual output is for example codedquote'replaced space as i said it already does what it has to do, but i know it can be improved, i hope you guys help me since in my country these kind of questions cant be answered without a ton of difficulties
what it does right now:
what the user writes would look like this
the result would look like this
If I understand correctly you want to take href from below:
<a class="linknpc" href="url/in/url/url/The%27White_Mob">
and expected output is
The'White_Mob
after you get the href and lets say the var enlace looks like below.
var enlace = "url/in/url/url/The%27White_Mob"
Below will first use String split on '/' to get all sections from href and from resultant array get the last element by pop() and use decodeURIComponent to decode the encoded uri.
var ans = decodeURIComponent(enlace.split('/').pop())
ans would now have the value: The'White_Mob
note: If the href ends with '/' then you need to adjust above solution accordingly

String separation and combining

I have a challenge that I am trying to solve and cannot seem to come up with a solid solution.
I have 3 records like this:
"a,b,c,d,e","1,2,3,4,5","record 1"
"f,g,h,i","11,12,15,16","record 2"
"x,y,z","19,20,21","record 3"
Looking to create this output
"a,1"
"b,2"
"c,3"
"d,4"
"e,5"
...
...
"y,20"
"z,21"
How would I go about this using java-script or
I have have worked out how to separate each string but collecting both substrings and looping through seems to be a challenge.
This can get a little bit Tricky if you dont have a constant format.
In this example i have two strings and each sub-string is separated by ",".
Also both strings should be the same length.
I return the result as an array here. You can append it to a existing string instead.
s1 = "1,2,3,4,5";
s2 = "a,b,c,d,e";
s3 = [];
for(i=0; s1.length>i;i++){
if(s1[i] != ","){
s3.push(s1[i]+","+s2[i]);
}
}
console.log(s3);
Since im not sure if i did understand the Question correctly...
If you dont understand something or the answer is not what you excepted, let me know in the comments and i will try to adapt to it.

How to use `%value%` in javascript?

I am learning JavaScript and I see %value% in a code but I do not know what does it mean or how to use it. Can anyone please help me explain to me. Thank you very much.
var formattedLocation = HTMLworkLocation.replace("%data%", work.jobs[job].location);
"%data%" is just a literal string. This code will take the value of HTMLWorkLocation, look for the first occurrence of %data% in it, and replace that with the value of work.jobs[job].location, and store the resulting string in formattedLocation.
var work = {
jobs: [{
location: "Home office"
}]
};
var job = 0;
var HTMLworkLocation = "John is located at %data%";
var formattedLocation = HTMLworkLocation.replace("%data%", work.jobs[job].location);
console.log(formattedLocation);
This is probably part of a template system that's used to replace placeholders like %data% with values that come from a table.
You're using string.replace which takes a string or regular expression as it's first argument. Based on the code you posted it looks like you're looking for the string "%data%" (or whatever string you're looking for) in HTMLworkLocation and replacing it with the value in work.jobs[job].location. Then it is being stored in formattedLocation.
I would put a debugger; line after that line of code so you can see what the values are in the debugger console. That might help make more sense of things.
Here is more info on the str.replace method with some examples

Meteor,issues with regex in mongodb

Hi i'm using mongodb and i=have few problems with mongodb regex
I have a collection with few fields and i want to search for a string in mongodb
when i enter this in my browser console,it is working fine and returning the count
posts.find({Headline: /google/i}).count()
it is returning the count which contains google as part of their headline
i tried this thing in my code and it is not returning count 0
var s_string="\/"+search_text+"\/";
var ss=Jobs.find({"J_Headline":search_text}).count();
console.log(ss);
whenever i give the full string of headline it is returning results
I don't know basics in regex help me with this.
Thx in advance
The search needs to be a RegExp object. Try this:
var search = new RegExp(search_text, 'i');
var ss=Jobs.find({'J_Headline': search}).count();
console.log(ss);
I think you also should check very good solution http://matteodem.github.io/meteor-easy-search/, it also have elastic search support
This is how regex can be used with MongoDB
db.<collection>.find({<field>: {$regex: /<key./}}).pretty()

Javascript to jquery issue

I made a pure JavaScript file the past days. It's a fully operational BlackJack game. However i was told to try and use as much jquery as possible on my existing file. So I can understand the basics of jQuery.
Things like
$("#Result").empty();
and
$("#hit").attr("disabled", "disabled");
$("#hit").removeAttr("disabled");
all work fine. But i have a problem with this one for example:
function player_blackjack() {
$("#Result").text("BlackJack!");
var bet = parseInt($("#bet").val());
var bank = parseInt(document.getElementById("Bank").innerHTML);
bank = bank + (bet * 2.5);
document.getElementById("Bank").innerHTML = bank;
start_game_buttons();
}
The first lines work, all except the var bank and the line above start_game_buttons();, thats why they are still javascript written.
If i change the var bank line to this one:
var bank = parseInt($("#Bank").html);
It just doesn't work. When i run this and make myself get a blackjack ill get a NaN where the bank should be. I also tried:
var bank = parseInt($("#Bank").val());
Im basically just trying whatever i can find, but since i can't figure it out im trying my luck here.
Thanks in advance!
You forgot the () after the html method, it should be:
var bank = parseInt($("#Bank").html());
Although, why are you using parseInt on an html() call? If this is an input -- use .val()

Categories