我有一个字符串,其中包含文本JavaScript And PHP
。我得到的H
字符的位置是17
。我已经得到了H的字符位置,但是我现在想要的是获得在位置17
处具有H字符的字。这是可能的Java脚本/ jQuery。如果是的话,请帮助我找到17
处有一个字符的单词。我怎样才能通过使用字符位置
-1
A
回答
0
循环之前和之后可以做这个任务。该代码是用来
<script>
var string = "JavaScript And PHP";
var k = string.substr(0,17);
var n = k.split(" ");
var se = n[n.length - 1];
var ke = string.substr(17,string.length);
var n = ke.split(" ");
var see = n[0];
alert(se+see);
</script>
JS FIDDLE:http://jsfiddle.net/56a7711y/
1
通过H之前的字符循环,直到你到达一个空白处,然后从空白处开始循环(即PHP中的P)并遍历该字直到你到达一个空间:)
var stringOld = "javascript and php";
var space = 0;
for(x=17; x>0; x--){
if(stringOld [x] === " ")
{
space = x;
break;
}
}
for(y=space; y<stringOld.length; y++){
if((stringOld[y] === " ") ||(y=stringOld.length))
{
var newString = stringOld.slice(space,y);
console.log(newString);
//alert(newString);
}
}
的jsfiddle:http://jsfiddle.net/edrcfq8a/1/
0
我的想法
$('#check').on('click',function(){
var t = $('#text').val();
var a = t.split(' ');
var pos = Number($('#positions').val());
var cha = $('#char').val();
$('#list').html('');
a.forEach(function(x){
var $p = x.indexOf(cha,pos)
if(x.indexOf(cha,pos-1) == pos-1){
$('#list').append('<li>"' + x + '" position of "' + cha +'" = ' + (x.indexOf(cha,pos-1) + 1) +'</li>');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="text" rows="4" cols="100">I have a string which contain the text JavaScript And PHP. I get the position of H character that is 17. I have get the character position of H but what i want now is to get the word which has H character at position 17. Is this possible with java script/jQuery. If yes please help me to get the word in which there is a character at position 17.</textarea>
position : <input type="text" id="positions" value="5" /> <br/>
char : <input type="text" id="char" value="h" /> <br/>
<button id="check"> check word</button>
<ul id=list>
<li></li>
</ul>
相关问题
- 1. 我怎样才能通过这样的字符串http.post angular2
- 2. 怎样才能通过使用组由
- 3. 我怎样才能通过字符串'发言'var
- 4. 我怎样才能通过随机
- 5. 我怎样才能通过零
- 6. 我怎样才能通过Java
- 7. 我怎样才能通过CGLIB
- 8. 我怎样才能从通过DatagramSocket的
- 9. 我怎样才能通过JavaScript函数
- 10. 我怎样才能从WSDL通过BEPL
- 11. 我怎样才能通过代码
- 12. 绕过中心位置,我怎样才能跳过中线?
- 13. 我怎样才能删除无效查询字符串使用PHP头位置
- 14. 我怎样才能从字符串
- 15. 我怎样才能子串字符串?
- 16. 我怎样才能打印字符值?
- 17. 我怎样才能从字符串
- 18. 我怎样才能绝对通过jQuery定位元素?
- 19. Android:我怎样才能找到使用PINCODE的位置?
- 20. 我怎样才能找到一个位置附近的位置?
- 21. 我怎样才能使PHP
- 22. 我怎样才能通过使用引导网格?
- 23. 使用ASP.NET,我怎样才能通过同一个对话框
- 24. 使用GNU调试器,我怎样才能通过__asm__语句?
- 25. 我怎样才能通过使用laravel加入
- 26. 我怎样才能让文字在大写的位置?
- 27. Python 2.7,我怎样才能找到字符串中的字母组的位置?
- 28. 我怎样才能
- 29. 我怎样才能
- 30. 我怎样才能
字和字是不同的,如果你想这样的事情再取字符启动从17到空白空间来了,或者使用常规表达式 – 2015-02-12 09:10:52
@ Arunprasanth KV可以从哟你的评论我发现它怎么可能 – 2015-02-12 09:12:22
@Hudixt这是否有帮助.. http://jsfiddle.net/d4tum8zy/ – 2015-02-12 09:18:45