Passing a param to param - javascript

I have the following:
html = html.replace(/[\d\.]+/g, "");
I want to the get the value of /[\d\.]+/g and put it in between the "" and then add some over jibberish after it.
Is this possible? If so how? Whats the term called of passing a value from parameter 1 to 2?

You want to reference a match from the first parameter? Fairly simple.
First, a "match" is defined inside parenthesis. This way we say "this is the first group." So, you want to match the entire string, so let's put everything between the start and end slash in parenthesis:
/([\d\.]+)/g
Now, we reference these past matches with $# where #, starting at 1, is the order in which they appear. So, our final replacement looks like this:
html = html.replace(/([\d\.]+)/g, "$1 your extra content here");
Where, as you can see, you can define your extra content.

Related

grab value of custom attribute

Is it possible to grab custom attribute like this 'something-20'
say for example it is in <div class="somecustomClass something-20"></div>
I want to grad the 19 so that I can manipulate it, because the css has block from something-1 to something-100
I used below code to retrieve tab id :
tabId = $('li').find('a').attr('href').replace('#tab', '');
is it the same approach?
That's not a custom attribute, it's a class. You'd have to get the entire class string, then probably use a regular expression to find the value you want.
It would be easier to use data- attributes:
<div class="somecustomClass" data-something="20"></div>
JS:
var value = $('.somecustomClass').data('something'); // 20
If you want it to be a custom attribute I suggest you do what Jason said in his answer. But if you want to grab the something-# elements and do something with them you can do the following.
for(var i=1;i<=100;i++) {
var el = $('.something-'+i);
//do something with the element to manipulate it
}
Similar. What the tab id thing does is 3 things
Part 1 is selecting the right element.
Part 2 is getting the value of the attribute that contains the data we want
Part 3 is getting the specific bit of the data that we want with a regular expression
For part 1, I'm not sure what you're using to identify these blocks in order to select them.
You could have $('[class^="something"]') to get all the elements that have a class that starts with the text 'something', but that will be quite slow. If you can use something like $('.somecustomClass') it will perform better.
If you just wanted to adapt the first matching element you came across, you could do this:
var myNumber = $('.somecustomClass')[0].className.replace(/.*?\bsomething\-(\d+).*/gi, "$1");
Apologies if you are already familiar with regular expressions, but for other readers this is a breakdown of what it does:
.*? means non-greedily select zero or more characters, \b means word boundary, then it finds the text 'something-' followed by one or more digits. Putting brackets around it captures what it finds there. Just in case you have classes after than, it has .* to get zero or more characters to find them too. /gi on the end of that means look globally through the class and i means be case-insensitive. $1 as the second argument of the replace function is the captured digits.

How can I search for {{.*}} and replace with json

I am trying to create my own javascript simple template function
I want to create a html page that will look like this
<p>
{{HELLO_WORLD}}
<br />
{{MY_NAME_IS}}
</p>
and than with javascript to replace anything that is in {{}}
with a json var that will look like this
{HELLO_WORLD: "Hello World!", MY_NAME_IS: "My name is"}
I am a little confused about the right method to do this.
the point is to make a multilanguage web site, that way I load the json for the desired language.
thank's.
JavaScript supports regular expression-based find-and-replace, with functions for the replacement. So you can do this:
myInputString.replace( /\{\{([^\}]*)\}\}/g, function( s, v ) { return myJSON[v] } );
To explain:
replace takes 2 arguments. The first is a regular expression object. In this case we build one inline using JavaScript's /expression/flags syntax. It looks for 2 opening braces (which need to be escaped because they have special meaning in regular expressions) followed by any characters which are not a closing brace, followed by 2 closing braces. The g means "global", so that it will match all cases rather than just the first one.
When a match is found, the function will be called. The first argument (I called it s) is the full matched string (like "{{abc}}"), the second (I called it v) is set to the first bit in brackets (like "abc").
In real code, you should add error checking (variables which don't exist), and possibly convert to lowercase / whatever.
Full details on replace are here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

Simple javascript regexp to extract a part starting and ending with a letter

I'm trying to get this regexp to return, and only return "flies a"
s = 'test <!-- flies are little birds --> end';
alert(s.match(/f(.*)a/));
Why does this return
flies a
lies
How can I make the f and a obligatory?
Thank you
.match returns an array. The first element is the whole matched text and each other element is the content of a capture group. You have one capture group, so the second element is the text captured by it.
Either remove the group or just ignore the other elements in the array. You have to access the first one in any case:
var match = s.match(/f(.*)a/)[0]; // or /f.*a/
P.S.: alert is a bad debugging tool, use console.log or console.dir instead.
Don't put in the brackets.
alert(s.match(/f.*a/));
The brackets create a capture group, which lets you specify that you're interested in particular parts of the match. In this case you're interested in the entire thing so you don't need any brackets.
Just remove parentheses or place the whole expression into additional parentheses:
s.match(/f.*a/)[0];
or
s.match(/(f(.*)a)/)[1];

How to match between characters but not include them in the result

Say I have a string "&something=variable&something_else=var2"
I want to match between &something= and &, so I'll write a regular expression that looks like:
/(&something=).*?(&)/
And the result of .match() will be an array:
["&something=variable&", "&something=", "&"]
I've always solved this by just replacing the start and end elements manually but is there a way to not include them in the match results at all?
You're using the wrong capturing groups. You should be using this:
/&something=(.*?)&/
This means that instead of capturing the stuff you don't want (the delimiters), you capture what you do want (the data).
You can't avoid them showing up in your match results at all, but you can change how they show up and make it more useful for you.
If you change your match pattern to /&something=(.+?)&/ then using your test string of "&something=variable&something_else=var2" the match result array is ["&something=variable&", "variable"]
The first element is always the entire match, but the second one, will be the captured portion from the parentheses, which is much more useful, generally.
I hope this helps.
If you are trying to get variable out of the string, using replace with backreferences will get you what you want:
"&something=variable&something_else=var2".replace(/^.*&something=(.*?)&.*$/, '$1')
gives you
"variable"

Find Replace Short Part of URL using Javascript

Assume I have the following URL stored in variable called content:
http://www.example.com/watch?v=4444444&feature=related
Problem:
I need to replace watch?v= with embed/
I need to erase whatever comes after &
The final output would look like:
http://www.example.com/embed/4444444
I tried these two steps but didn't work:
content = content.replace('/watch?v=/', 'embed/');
content = content.replace('&*/g','');
The URL in page source code appears as:
http://www.example.com/watch?v=4444444&feature=related
You have many errors:
You are using a regular expression when you only need a string.
You are writing your regular expressions as strings.
To write 'match any characters' you need to write '.*', not just '*'. The star modifies the previous token.
There is no need to use the g flag here.
Try this instead:
content = content.replace('watch?v=', 'embed/').replace(/&.*/, '');

Categories