Javascript ways or Method(s) to get adjacent characters in string
Trying to get adjacent characters in a string including blank spaces. the 3 characters to the left and the 3 characters to the right...
maybe using regular expressions and JavaScript split() to get adjacent characters in string?
var str = "one two three four five",
array = str.split("");
but missing the blank spaces and the 3 character to the left and开发者_开发技巧 the 3 characters to the right...
any suggestions?
Did you try JS substring
Something like this
var str="Hello world!";
document.write(str.substring(3)+"<br />");
document.write(str.substring(3,7));
http://www.w3schools.com/jsref/jsref_substring.asp
Or something like finding position
http://phpjs.org/functions/strpos:545
I assume you're looking for a combination of indexOf and substr
Here's an example function that returns an object containing left, right and error (if any)
function findSurrounding(needle, haystack, leadingCharacters, trailingCharacters){
var pos = haystack.indexOf(needle);
var returnObject = {'left':'', 'right':'', 'error':null};
//if needle was not found, output error
if(pos == -1){
returnObject.error = needle + ' could not be found!';
//otherwise set "left" and "right"
} else {
returnObject.left = haystack.substr(pos - leadingCharacters, leadingCharacters);
returnObject.right = haystack.substr(pos + needle.length, trailingCharacters);
}
return returnObject;
}
//get output:
var surrounding = findSurrounding('three', 'one two three four five', 3, 3)
//debug / write output
for(prop in surrounding){
document.write(prop+': '+surrounding[prop]+'<br/>');
}
output of this is
left: wo
right: fo
error: null
Hope that helps (additionally hope I understood your question!)
I'm adding another answer here because I believe my original answer really does answer your question as stated (how to get adjacent characters).
However after you posted your code I see you want to create a magnifier. That's kinda a different kettle of fish so I rewrote your code.
The reason it's different is then you need to use the mouseover to get individual letters and that means you must encapsulate individual letters into tags. The code posted does that (essentially a working version of your code.) Note I didn't mess with your draggable. If you're having issues with that time for a new question.
<style type="text/css">
body { font-family:"Tiresias PCfont Z"; /* font-size:15px; */ background: #fff; line-height:1.1em; }
.draggable { width: 90px; height: 90px; padding: 0.5em; float: left; margin: 0 10px 10px 0; }
#draggable { margin-bottom:20px; }
#MagDrag {background-image:url(controls/MagLens1.png); width: 236px; height: 73px; }
#containment-wrapper { width: 95%; height:150px; border:2px solid #ccc; padding: 10px; display:none;}
#magnifyView { width: 90px;
background-image:url(controls/MagLens1.png);width: 212px;height: 50px; white-space: nowrap; font-size: 40px; font-weight:bold;
}
.onText { color:#990000;}
</style>
<script type="text/javascript" charset="utf-8" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript" src="http://www.java2s.com/Tutorial/JavaScriptDemo/js/ui/ui.core.js"></script>
<script type="text/javascript" src="http://www.java2s.com/Tutorial/JavaScriptDemo/js/ui/ui.draggable.js"></script>
<script type="text/javascript" charset="utf-8">
//custom "magnify" function
var mag = function(){
return {
add: function(){
//encapsulate #article into individual spans
//why? mouseover events are triggered by tags, not individual letters
var str = $('#article').text();
var output = '';
for(var i = 0; i<str.length; i++){
output += '<span>' + str.substr(i, 1) + '</span>';
}
//replace #article with altered html
$('#article').html(output);
//add the mouseover
$('#article span').mouseover(function(){
//note both these selectors could be way more efficient
//however this should get you started...
var leading = $(this).prev().add($(this).prev().prev()).add($(this).prev().prev().prev());
var trailing = $(this).next().add($(this).next().next()).add($(this).next().next().next());
$('#magnifyView').html(
$(leading).text() +
'<span class="onText">' + $(this).text() + '</span>' +
$(trailing).text()
);
});
$('#containment-wrapper').show();
},
remove: function(){
$('#article span').unbind('mouseover');
$('#article').html($('#article').text());
$('#containment-wrapper').hide();
}
}
}();
// jquery $ function load
$(document).ready(function(){
$('.addmag').click(function(){
mag.add();
});
$('.removemag').click(function(){
mag.remove();
});
$("#magnifyView").draggable({ containment: '#containment-wrapper', scroll: false, axis: 'x' });
});
</script>
Note, you'll need to click the "addmag" or "removemag" to add or remove the magnifier functionality. (This is there to demonstrate how to encapsulate a string using jquery and a basic loop as well as how to get the original string back, preserving spaces etc.)
Hope that helps!
精彩评论