Javascript 方法或方法来获取字符串中的相邻字符
尝试获取字符串中包含空格的相邻字符。左侧 3 个字符和右侧 3 个字符...
也许使用正则表达式和 JavaScript split() 来获取字符串中的相邻字符?
var str = "one two three four five",
array = str.split("");
但缺少空格以及左侧 3 个字符和右侧 3 个字符...
有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你尝试过JS子字符串吗
http://www.w3schools.com/jsref/jsref_substring.asp
或者类似查找位置
http://phpjs.org/functions/strpos:545
Did you try JS substring
Something like this
http://www.w3schools.com/jsref/jsref_substring.asp
Or something like finding position
http://phpjs.org/functions/strpos:545
我假设您正在寻找 indexOf 和 substr 的组合
,这是一个示例函数,它返回一个包含左、右和错误(如果有)的对象,
其输出
希望有所帮助(另外希望我理解您的问题!)
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)
output of this is
Hope that helps (additionally hope I understood your question!)
我在这里添加另一个答案,因为我相信我原来的答案确实回答了您的问题(如何获取相邻字符)。
但是,在您发布代码后,我看到您想创建一个放大镜。这有点不同,所以我重写了你的代码。
不同的原因是您需要使用鼠标悬停来获取单个字母,这意味着您必须将单个字母封装到标签中。发布的代码就是这样做的(本质上是代码的工作版本。)注意我没有弄乱你的可拖动。如果您对新问题的时间有疑问。
请注意,您需要单击“addmag”或“removemag”来添加或删除放大镜功能。 (这是为了演示如何使用 jquery 和基本循环封装字符串,以及如何获取原始字符串、保留空格等。)
希望有所帮助!
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.
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!