简体   繁体   English

如何找到字符串某个部分开始的位置?

[英]How can I find the position where a certain part of a string begins?

It's late, I'm really tired, I'm having a lot of trouble figuring out a simple thing. 已经很晚了,我真的很累,我在搞清楚一件简单的事情时遇到了很多麻烦。

I have a string which will be of the form 我有一个形式的字符串

BROADCAST FROM x\ny\nz

...where x is a single word with no spaces or newlines, y is a number, and z is a string whose length in characters is y . ...其中x是单个单词,没有空格或换行符, y是数字, z是字符串长度为y的字符串。

All I need is to find the starting index of z so that I may take string.slice(indexOfZ) instead . 我只需要找到z的起始索引,以便我可以改为使用string.slice(indexOfZ)

What is the form of this regular expression, and how would I write it in JavaScript? 这个正则表达式的形式是什么,我将如何用JavaScript编写它?

It should be to the effect of... 应该是......的效果

pattern = /\n[0-9]{1,}\n.*/;
var index = string.match(pattern);

Am I wrong somewhere? 我错了吗?

为什么/^BROADCAST FROM \\w+ \\d+ (.+)$/不起作用?

Regexes are good and all, but in this instance why make it hard for yourself. 正则表达式很好,但在这种情况下为什么让自己变得困难。

var aParts = sFullString.split("\n");

Edit : Looks like u just want z so : 编辑:看起来你只想要z所以:

var z = aParts[2];

one way to find this is split the string by / 找到这个的一种方法是将字符串拆分为/

var myString = x\ny\nz;
var str =  myString.split('\'); //if the format is same.

var index = str[2].indexOf("z") . var index = str[2].indexOf("z")

There may be another solution. 可能还有另一种解决方案。 but this is one that currently comes in my mind. 但这是我脑海中的一个。

Hope this one helps. 希望这个有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM