从特殊字符前后的字符串中获取值 - Jquery [英] Get value from a string before and after a special character - Jquery

查看:34
本文介绍了从特殊字符前后的字符串中获取值 - Jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取变量文本中特殊字符前后的值.

Hi I want to read values before and after special character in text of a variable.

特殊字符总是相同的,例如'|'例如,我的文本将存储为

The special character will always be the same e.g., '|' For example, my text will be stored as

var username = Username|Jackie;
var password = Password|myPass;
var temp = ""

读完后,我将执行如下if语句

After reading the, I would be carrying out a if-statement as follows

IF text BEFORE | == USERNAME
   temp == text AFTER |
ESLE IF text BEFORE ==  Password
   temp == text AFTER |

我希望我说得通,如果没有,请告诉我:)

I hope I am making sense, if not, please let me know :)

我已经能够做到这一点,但它只读取特殊文本之后的文本,我想要之后和之前.

I have been able to do this, but it only reads text after the special text and I want both after and before.

var myString = someVariableName.val().split("|").pop();

https://jsfiddle.net/bf1L5e7y/1/

推荐答案

您可以在一行中使用 解构

一个工作示例

var [before, after] = 'Username|Jackie'.split("|")

console.log(before)
console.log(after)

上面和这样做是一样的:

The above is the same as doing this:

var pieces = 'Username|Jackie'.split("|")
var before = pieces[0]
var after = pieces[1]

console.log(before)
console.log(after)

这篇关于从特殊字符前后的字符串中获取值 - Jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆