Javascript:测试正则表达式并分配给变量,如果它在一行中匹配(Javascript: test regex and assign to variable if it matches in one line)
要测试正则表达式是否匹配并将其分配给变量(如果不匹配),或者将其分配给某个默认值(如果不匹配),我目前正在执行以下操作:
var test = someString.match(/some_regex/gi); var result = (test) ? test[0] : 'default_value';
我想知道是否有任何方法可以在JS中使用一行代码执行相同的操作。
澄清:我并不是想让我的代码更小,而是在我定义一些变量的地方使它更清晰:
var foo = 'bar', foo2 = 'bar2', foo_regex = %I want just one line here to test and assign a regex evaluation result%
To test if a regex matches and assign it to a variable if it does, or assign it to some default value if it doesn't, I am currently doing the following:
var test = someString.match(/some_regex/gi); var result = (test) ? test[0] : 'default_value';
I was wondering if there is any way to do the same thing in JS with one line of code.
Clarification: I am not trying to make my code smaller, but rather make it cleaner in places where I am defining a number of variables like so:
var foo = 'bar', foo2 = 'bar2', foo_regex = %I want just one line here to test and assign a regex evaluation result%
原文:
最满意答案
那么,我会说一个调试器将是你最好的朋友。 我也有点惊讶收到的结果给你显示的打印输出,也许更多的问题没有显示。
查看以下行的位置:
send(sSend, (char*)&num, sizeof(num), NULL);
并比较你接收到的地方:
recv(sRecv, (char*)&num, sizeof(num), NULL);
提示,你打电话发送超过recv。
Well, I'd say a debugger will be your best friend. I'm also a little surprised the received end gave the printed output you show, perhaps more issues that aren't shown.
Look at the location of the following line:
send(sSend, (char*)&num, sizeof(num), NULL);
And compare to where you have the receiving of that:
recv(sRecv, (char*)&num, sizeof(num), NULL);
Hint, you call send more than recv.
相关问答
更多-
在winsock关闭之前在Visual Basic 6中发送数据?(Sending data before winsock closes in Visual Basic 6?)[2022-02-26]
看看_SendComplete()事件 例如,使用表单级布尔值: Option Explicit Private mblnClosing As Boolean Private Sub Command1_Click() Winsock1.SendData "USERLEAVES" & txtUser.Text mblnClosing = True End Sub Private Sub Form_Load() mblnClosing = False End Sub Private Sub ... -
没有充分的理由不使用STL容器,因为它们是C ++的核心,因此它们将在每个C ++编译器中可用。 但是,要回答你的问题,如果你因任何原因需要避免它们,你可以这样做: int iResponseCapacity = MAX_BUFFER*5; int iResponseLength = 0; char *response = new char[iResponseCapacity]; do { iRecv = recv(m_socket, recvBuffer, MAX_BUFFER, 0); ...
-
如何使用Winsock 2?(How to use Winsock 2?)[2022-04-08]
你需要在windows.h之前包含winsock2.h You need to include winsock2.h before windows.h -
通过UDP winsock连接发送结构,结构的大小?(Sending structs over a UDP winsock connection, size of struct?)[2023-10-04]
你已经接到UDP的好东西有时会以任意方式重新排序消息。 总是发送包含所有可能信息的相同结构的解决方案几乎是正确的: 定义自己的基本消息,例如: struct msg_common{uint8_t sender, receiver; uint16_t message_id;}; // Take note that I used fixed-width types here! 并使所有特定消息以此开头,为每个消息保留公共前缀的非重叠范围。 您无法确认消息是否有效,这意味着: 它完整,又名。 UDP数据包包含整 ... -
我设法回答了! 我所做的是右击项目 - >属性 - >链接器 - >其他选项并在那里写-lws2_32。 我之前做的是项目 - >属性 - > c ++编译器 - >其他选项并在那里写-lws2_32。 I managed to answer it! What I did was right click on project -> properties -> linker -> Additional options and write -lws2_32 there. What I was doing bef ...
-
数据可能需要多个recv呼叫才能完全接收。 发生这种情况时, Receive_Full_Packet会为每个此类调用分配一个新的缓冲区,并立即泄漏除其中一个之外的所有缓冲区。 它返回给调用者的是最后一个这样的分配 - 一个大多数未初始化的缓冲区,其中包含了随机垃圾,最后一块数据在最后。 Move isBuff = new char[Size]; 在循环外部,将它放在第一次调用调用大小的调用之后。 Finally Figured it out. Turns out there was absolutely n ...
-
C ++ WinSock错误10053(C++ WinSock error 10053)[2022-03-04]
您假定在收到每个HTTP响应后连接保持打开状态,但不能保证。 您必须查看服务器的实际HTTP响应(特别是在状态行中报告的HTTP版本以及Connection标题处)以了解服务器是否将连接断开。 您正在发送HTTP 1.1请求,但不能保证服务器将回复HTTP 1.1响应。 如果响应使用HTTP 0.9,则在发送下一个HTTP请求之前,您必须关闭套接字并重新连接。 如果响应使用HTTP 1.0,并且Connection标头没有说明keep-alive ,那么在发送下一个HTTP请求之前,您必须关闭套接字并重新连 ... -
你没有分配足够的内存。 你的buffer大小只有1个char ( sizeof(char)是1个字节),但你试图填充12个字节( sizeof(Sid)是4个字节, sizeof(SLogin)是8个字节)。 即使你确实分配了足够的内存,它仍然无法工作,因为SLogin包含指向外部内存的指针,你没有将其序列化为可以安全发送/ SLogin的连续格式。 尝试更像这样的东西: #pragma pack(push, 1) // or equivilent for your compiler struct Sid ...
-
使用winsock发送结构(sending structure with winsock)[2022-01-09]
这个: amountToSend = sprintf_s(serializedPacket, sizeof(buffer), (char*)&p); 根本没有任何意义。 sprintf_s()的第三个参数是格式化字符串,但是您将指针传递给结构,重新转换为字符指针。 这绝对没有意义。 将结构表示为文本字符串(序列化为字符串)可能是个好主意,但是您需要一个正确的格式化字符串: amountToSend = sprintf_s(serializedPacket, sizeof(buffer), "%d %s", ... -
发送向量Winsock问题(Sending Vector Winsock Problems)[2022-09-10]
那么,我会说一个调试器将是你最好的朋友。 我也有点惊讶收到的结果给你显示的打印输出,也许更多的问题没有显示。 查看以下行的位置: send(sSend, (char*)&num, sizeof(num), NULL); 并比较你接收到的地方: recv(sRecv, (char*)&num, sizeof(num), NULL); 提示,你打电话发送超过recv。 Well, I'd say a debugger will be your best friend. I'm also a little sur ...