I have some javascript that I need assistance with where I want to update a string with javascript.
Original string:
987654321-200x200-1_This+is+text.jpg
Want it to end up to be:
not_found-200x200.jpg
So 987654321 gets replaced with not_found and -1_This+is+text with nothing.
Note the original string is fully dynamic with only the - x - _ + constant in all.
I have tried something like this:
'987654321-200x200-1_This+is+text.jpg'.replace(/\_\d{0,}[A-Za-z]*/, '_not_found')
but need help with the regexp to achieve this. Anyone help?
Not sure if this will do, but if all you're looking for is 200x200 you could just split on - and use that :
var str = '987654321-200x200-1_This+is+text.jpg';
var not = 'not_found-' + str.split('-')[1] + '.jpg';
Related
Let's say I have this String : str = '236112456'
I want to change '236' with something else, and if a character is alone, change by another thing.
i tried something like that :
var result =
str.replaceAll('236', '236.jpg')
.replaceAll('1', '1.jpg')
.replaceAll('2', '2.jpg')
.replaceAll...
but it won't take 236 and only change individual letters...
How can I achieve that ?
Thanks in advance for your help :)
[edit] - Mistakes in the code example
Here is the solution.
Please use regex expression instead of '23' string.
str = '236112456';
var result = str.replaceAll(/236/gi, '236.jpg').replaceAll(/1/gi, '1.jpg');
...
The result is "236.jpg1.jpg1.jpg2456". Is this not what you are looking for ?
I've seen several results for removing characters after a specific character - my question is how would I do that with a string?
Basically, this applies to any given string of data, but let's take a URL: stackoverflow.com/question
With given string, and in JS, I'd like to remove everything after ".com", assign ".com" to a variable, and assign the text before ".com" to a separate variable.
So, end result: var x = "stackoverlow" var y = ".com"
What I've done so far:
1) Using a combination of split, substring, etc. I can get it to remove pieces, but not without removing part of the ".com" string. I'm pretty sure I can do what I want to do with substring and split, I think I'm just implementing it incorrectly.
2) I'm using indexOf to find the string ".com" within the full string
Any tips? I haven't posted my actual code because it's become so garbled with all the different things I've tried (I can go ahead and do so if necessary).
Thanks!
You should really look into Regular Expressions.
Here is some code that can get what you are trying to do:
var s = 'stackoverflow.com/question';
var re = /(.+)(\.com)(.+)/;
var result = s.match(re);
if (result && result.length >= 3) {
var x = result[1], //"stackoverlow"
y = result[2]; //".com"
console.log('x: ' + x);
console.log('y: ' + y);
}
Use regular expressions.
"stackoverflow.com".match(/(.+)(\.com)/)
results in
["stackoverflow.com", "stackoverflow", ".com"]
(Why would you want to assign .com to a variable, though?
"stackoverflow.com".split(/\b(?=\.)/) => ["stackoverflow", ".com"]
Or,
"stackoverflow.com/question".split(/\b(?=\.)|(?=\/)/)
=> ["stackoverflow", ".com", "/question"]
I'm attempting to parse a text string with jQuery and to make a variable out of it. The string is below:
Publications Deadlines: armadllo
I'm trying to just get everything past "Publications Deadlines: ", so it includes whatever the name is, regardless of how long or how many words it is.
I'm getting the text via a the jQuery .text() function like so:
$('.label_im_getting').text()
I feel like this may be a simple solution that I just can't put together. Traditional JS is fine as well if it's more efficient than JQ!
Try this,
Live Demo
First part
str = $.trim($('.label_im_getting').text().split(':')[0]);
Second part
str = $.trim($('.label_im_getting').text().split(':')[1]);
var string = input.split(':') // splits in two halfs based on the position of ':'
string = input[1] // take the second half
string = string.replace(/ /g, ''); // removes all the spaces.
I have a string from which I am trying to get a specif value. The value is buried in the middle of the string. For example, the string looks like this:
Content1Save
The value I want to extract is "1";
Currently, I use the built-in substring function to get to remove the left part of the string, like this:
MyString = "Content1Save";
Position = MyString;
Position = Position.substring(7);
alert(Position); // alerts "1Save"
I need to get rid of the "Save" part and be left with the 1;
How do I do that?
+++++++++++++++++++++++++++++++++++++++++
ANSWER
Position = Position.substr(7, 1);
QUESTION
What's the difference between these two?
Position = Position.substr(7, 1);
Position = Position.substring(7, 1);
You can use the substr[MDN] method. The following example gets the 1 character long substring starting at index 7.
Position = Position.substr(7, 1);
Or, you can use a regex.
Position = /\d+/.exec(Position)[0];
I would suggest looking into regex, and groups.
Regex is built essentially exactly for this purpose and is built in to javascript.
Regex for something like Content1Save would look like this:
rg = /^[A-Za-z]*([0-9]+)[A-Za-z]*$/
Then you can extract the group using:
match = rg.exec('Content1Save');
alert(match[1]);
More on regex can be found here: http://en.wikipedia.org/wiki/Regular_expression
It highly depends on the rules you have for that middle part. If it's just a character, you can use Position = Position.substring(0, 1). If you're trying to get the number, as long as you have removed the letters before it, you can use parseInt.
alert(parseInt("123abc")); //123
alert(parseInt("foo123bar")); //NaN
If you're actually trying to search, you'll more often than not need to use something called Regular Expressions. They're the best search syntax JavaScript avails.
var matches = Position.match(/\d+/)
alert(matches[0])
Otherwise you can use a series of substr's, but that implies you know what is in the string to begin with:
MyString.substr(MyString.indexOf(1), 1);
But that is a tad annoying.
I'm trying to use the replace function in JavaScript and have a question.
strNewDdlVolCannRegion = strNewDdlVolCannRegion.replace(/_existing_0/gi,
"_existing_" + "newCounter");
That works.
But I need to have the "0" be a variable.
I've tried:
_ + myVariable +/gi and also tried
_ + 'myVariable' + /gi
Could someone lend a hand with the syntax for this, please. Thank you.
Use a RegExpobject:
var x = "0";
strNewDdlVolCannRegion = strNewDdlVolCannRegion.replace(new RegExp("_existing_" + x, "gi"), "existing" + "newCounter");
You need to use a RegExp object. That'll let you use a string literal as the regex, which in turn will let you use variables.
Assuming you mean that you want the zero to be any single-digit number, this should work:
y = x.replace(/_existing_(?=[0-9])/gi, "existing" + "newCounter");
It looks like you're trying to actually build a regex literal with string concatenation - that won't work. You need to use the RegExp() constructor form instead, in order to inject a specific variable into the regex: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/RegExp
If you use the RegExp constructor, you can define your pattern using a string like this:
var myregexp = new RegExp(regexstring, "gims") //first param is pattern, 2nd is options
Since it's a string, you can do stuff like:
var myregexp = new RegExp("existing" + variableThatIsZero, "gims")